Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update embedded document in mongoose?
    primarykey
    data
    text
    <p>I've looked through the mongoose API, and many questions on SO and on the google group, and still can't figure out updating embedded documents.</p> <p>I'm trying to update this particular userListings object with the contents of args.</p> <pre><code>for (var i = 0; i &lt; req.user.userListings.length; i++) { if (req.user.userListings[i].listingId == req.params.listingId) { User.update({ _id: req.user._id, 'userListings._id': req.user.userListings[i]._id }, { 'userListings.isRead': args.isRead, 'userListings.isFavorite': args.isFavorite, 'userListings.isArchived': args.isArchived }, function(err, user) { res.send(user); }); } } </code></pre> <p>Here are the schemas:</p> <pre><code>var userListingSchema = new mongoose.Schema({ listingId: ObjectId, isRead: { type: Boolean, default: true }, isFavorite: { type: Boolean, default: false }, isArchived: { type: Boolean, default: false } }); var userSchema = new mongoose.Schema({ userListings: [userListingSchema] }); </code></pre> <p>This find also doesn't work, which is probably the first issue:</p> <pre><code>User.find({ '_id': req.user._id, 'userListings._id': req.user.userListings[i]._id }, function(err, user) { console.log(err ? err : user); }); </code></pre> <p>which returns:</p> <pre><code>{ stack: [Getter/Setter], arguments: [ 'path', undefined ], type: 'non_object_property_call', message: [Getter/Setter] } </code></pre> <p>That should be the equivalent of this mongo client call:</p> <pre><code>db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")}) </code></pre> <p>Running:</p> <pre><code>mongoose v1.8.1 mongoose-auth v0.0.11 node v0.4.10 </code></pre>
    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.
 

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