Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Objects in your <code>writing</code> array is treated as <a href="http://mongoosejs.com/docs/subdocs.html" rel="nofollow">sub/embedded-documents</a>. This means that they are assigned an <code>_id</code> when they are stored in the database.</p> <p>So, there is a few ways you can query <code>Blog</code>s based on these sub documents:</p> <pre><code>var ObjectId = mongoose.Types.ObjectId; // Assuming these objects are in the 'writing' array of a Blog var postObject = { post: 'some text', name: 10 }, postObjectWithId = { _id: new ObjectId(), post: 'some text', name: 10 }; // Example #1 Works! Blog.find({'writing.post': postObject.post}) .exec(function(err, res){ onBlogResult("Ex#1", err, res) }); // Example #2 Works! Blog.find({}) .where('writing').in([postObjectWithId]) .exec(function(err, res){ onBlogResult("Ex#2", err, res) }); // Example #3 Works - its the same as example #2! Blog.find({'writing': { $in: [ postObjectWithId ]}}) .exec(function(err, res){ onBlogResult("Ex#3", err, res) }); // Example #4 Fails because of missing _id on postObject! Blog.find({'writing': { $in: [ postObject ]}}) .exec(function(err, res){ onBlogResult("Ex#4", err, res) }); </code></pre> <p>As you see, you can only <code>find</code> objects with an array containing an element using <code>in</code> if you have the full object (including the <code>_id</code>).</p> <p>You can view the whole source on this <a href="https://gist.github.com/jmyrland/5578516" rel="nofollow"><strong>GIST</strong></a> - Test it out for yourself :)</p> <p>Hope this helps :)</p> <p><a href="http://mongoosejs.com/docs/queries.html" rel="nofollow"><strong>Read the mongoose docs here</strong></a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload