如何更新猫鼬中的嵌入文档?
我已经浏览了 mongoose API,以及关于 SO 和 google group 的许多问题,但仍然无法弄清楚更新嵌入式文档。
我正在尝试使用 args 的内容更新这个特定的 userListings 对象。
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);
});
}
}
以下是架构:
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]
});
这个查找也不起作用,这可能是第一个问题:
User.find({
'_id': req.user._id,
'userListings._id': req.user.userListings[i]._id
}, function(err, user) {
console.log(err ? err : user);
});
返回:
{ stack: [Getter/Setter],
arguments: [ 'path', undefined ],
type: 'non_object_property_call',
message: [Getter/Setter] }
这应该相当于这个 mongo 客户端调用:
db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")})
运行:
mongoose v1.8.1
mongoose-auth v0.0.11
node v0.4.10
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您已经拥有用户时,您可以执行以下操作:
如下所示: http://mongoosejs.com/docs/subdocs .html
* * warning * *
正如 @zach 指出的,您必须在实际文档的架构之前声明子文档的架构才能使用
id()
方法。when you already have the user, you can just do something like this:
as found here: http://mongoosejs.com/docs/subdocs.html
* * 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.这只是变量名称不匹配吗?
for
循环中有user.userListings[i].listingId
,但find 中有
。user.userListings[i]._id
您是在寻找
listingId
还是_id
?Is this just a mismatch on variables names?
You have
user.userListings[i].listingId
in thefor
loop butuser.userListings[i]._id
in thefind
.Are you looking for
listingId
or_id
?您必须保存父对象,并标记修改嵌套文档。
我们就是这样做的
You have to save the parent object, and markModified the nested document.
That´s the way we do it