Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you are using <a href="http://mongoosejs.com/docs/populate.html" rel="nofollow">references</a> instead of <a href="http://mongoosejs.com/docs/subdocs.html" rel="nofollow">subdocuments</a>, you need to create the hashtag objects first:</p> <pre><code>var tagnames = ['a','b','c']; var hashtags = {}; //for referencing quickly by name later for (var h in tagnames){ var tag = new Hashtag({ name: tagnames[h] }); tag.save(function (err, item) { if (err) console.log('error:',err); hashtags[item.name] = item; }); } </code></pre> <p>Once you have the hashtags created, you can reference them::</p> <pre><code>var item = new Item({ hashtags: [hashtags.a._id,hashtags.b._id,hashtags.c._id] }); item.save(function (err, item) { if (err) return res.json({ error: err }); res.json(item); }); </code></pre> <p>Then you can use <a href="http://mongoosejs.com/docs/populate.html" rel="nofollow">populate</a> to automatically turn the object ids into documents:</p> <pre><code>Item.find({}) .populate('hashtags') .exec(function (err, items) { if (err) return handleError(err); //items are populated with hashtags }); </code></pre> <p>If you are just doing simple tagging, then <a href="http://mongoosejs.com/docs/subdocs.html" rel="nofollow">subdocuments</a> may be a better fit. They allow you to declare and save child documents all in one step. The trade-off is that subdocuments belong exclusively to their parent documents. They are not references so any aggregations on them must be done manually.</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. This table or related slice is empty.
    1. 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