0

So I have these 2 schemas and it won't let me have one object only but it returns the whole array of objects instead

const ReqEmbSchema = new mongoose.Schema({
    appId: String,
    lname: String,
    fname: String,
    midname: String
});



const RequestSchema = new mongoose.Schema({
    appId: String,
    userRequest: [ReqEmbSchema]
});

I tried doing this to get 1 specific embedded object from ReqEmbSchema

 const update = "zDfXUoQhc8";
Request.findOne({'userRequest.appId': update}, function(err,foundRequest){
            console.log(foundRequest);
        });

But sadly it returns all embedded objects in my log. Please help me fix .

  • set projection in second parameter `.findOne({'userRequest.appId': update}, { "userRequest.$":1 }, your callback)` – turivishal Jan 31 '21 at 16:19
  • Does this answer your question? [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – turivishal Jan 31 '21 at 16:22
  • Almost, it only returns this ` [ { userRequest: [ [Object] ] } ] ` I want to get the values of of that embedded object – tomato_ketch Jan 31 '21 at 16:57

0 Answers0