Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

mongodb - How to update embedded document in mongoose?

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.

I'm trying to update this particular userListings object with the contents of args.

for (var i = 0; i < 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);
        });
    }
}

Here are the schemas:

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]
});

This find also doesn't work, which is probably the first issue:

User.find({
    '_id': req.user._id,
    'userListings._id': req.user.userListings[i]._id
}, function(err, user) {
    console.log(err ? err : user);
});

which returns:

{ stack: [Getter/Setter],
  arguments: [ 'path', undefined ],
  type: 'non_object_property_call',
  message: [Getter/Setter] }

That should be the equivalent of this mongo client call:

db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")})

Running:

mongoose v1.8.1
mongoose-auth v0.0.11
node v0.4.10 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you already have the user, you can just do something like this:

var listing = req.user.userListings.id(req.params.listingId);

listing.isRead = args.isRead;
listing.isFavorite = args.isFavorite;
listing.isArchived = args.isArchived;

req.user.save(function (err) {
  // ...
});

as found here: http://mongoosejs.com/docs/subdocs.html

Finding a sub-document

Each document has an _id. DocumentArrays have a special id method for looking up a document by its _id.

var doc = parent.children.id(id);

* * warning * *

as @zach pointed out, you have to declare the sub-document's schema before the actual document 's schema to be able to use the id() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...