2

I have somewhat the following schema(without _id) -

 {uid: String,
 inbox:[{msgid:String, someval:String}]
 }

Now, in the request I get the msgid and I use it in the following mongoose query like this-

 my_model.findOne({'inbox.msgid':'msgidvaluexyz'}
  , function(err, doc) {           
    console.log(doc);
    return !0; })

Now, the problem is that I get the whole document which has the specific message along with the other messages in inbox -

   Output-
   {uid:'xyz',
    inbox:[
     {msgid:,someval},
     {msgid:'our queried msgid',someval}, //required sub array
     {msgid:,someval},
      ]
       }

Now what query can i use to get the specific sub array only as the document inbox is too large to be looped through.

2 Answers 2

3

Use the $ positional selection operator to have the returned doc only include the matched inbox element:

my_model.findOne({'inbox.msgid':'msgidvaluexyz'}
  , {'inbox.$': 1}
  , function(err, doc) {           
    console.log(doc);
    return !0; })
Sign up to request clarification or add additional context in comments.

6 Comments

thx man..sure does look like it will get the work done.. will save some serious processing time.. :)
one quick quesn...the callback does return the specific sub array..but whn i try to change something and save the callback variable... say callback_doc.inbox[0].changed_var=something;callback_doc.save() .. it updates the changes to the first index of the array(index 0)..how do we refer to the specific index in the callback.. something like callback_doc.inbox[$] ?
@user597272 You don't have the whole object, so I think you'll want to update it via update rather than save and use the $ positional update operator which serves the same purpose on the update side.
...actually i was using save because in the mongoose documentation it said that save follows the routine integrity checks which update skips.. however i dont inderstand the behavior of save for the sub array... function(err, doc) { doc.save(function(err1, doc1) {})} .. here i change 'doc' and outputting 'doc1' does shows it as changed..but in mongo it updates the element at the '0th' position.. i change doc.inbox[0].changed_var bcoz doc only returns the '0th' inbox array..cant i use '$' in place of '0' ?
srry for the big post.. in short can I get the numeric index of the returned sub array?
|
0

If I understood your question correctly:

// find each person with a last name matching 'Ghost'
var query = Person.findOne({ 'name.last': 'Ghost' });

// selecting the `name` and `occupation` fields
query.select('name occupation');

http://mongoosejs.com/docs/queries.html

You can select which fields you want to get back. You could get only the inbox array or everything except that.

1 Comment

srry..but i think you didn't get my question properly...the 'inbox' field is an array, it will still have all the msg sub arrays..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.