Arrays 在meteor/mongodb中的数组中创建新对象(该对象位于文档数组中的对象中)
我在mongo db中创建新对象时遇到问题。 我正在使用meteor框架,我想添加新的子项, 到一个子项数组 我的数据结构如下所示:Arrays 在meteor/mongodb中的数组中创建新对象(该对象位于文档数组中的对象中),arrays,mongodb,nested,meteor,insert-update,Arrays,Mongodb,Nested,Meteor,Insert Update,我在mongo db中创建新对象时遇到问题。 我正在使用meteor框架,我想添加新的子项, 到一个子项数组 我的数据结构如下所示: { "_id" : "f9d01fd1ef22684353149851", "name" : "Ueberschrift", "items" : [ { "_id" : "be695ec7ffe71152088c57e1",
{
"_id" : "f9d01fd1ef22684353149851",
"name" : "Ueberschrift",
"items" : [
{ "_id" : "be695ec7ffe71152088c57e1",
"name" : "asd",
"checked" : false,
"subitems" : [ ]
},
{ "_id" : "fe665ec7ffe78852088c22g8",
"name" : "asd",
"checked" : false,
"subitems" : [ ]
}
]
},
{
"_id" : "qw501fd1we522683531qw451",
.........
}, ...
例如,我想添加子项
{ _id : 1, name : "MySubitemName" }
到id为“be695ec7ffe71152088c57e1”的项目
它位于对象“f9d01fd1ef22684353149851”中
我使用以下命令执行此操作:
uid => "f9d01fd1ef22684353149851" and
id => "be695ec7ffe71152088c57e1"
Listitems.update({_id:uid, "items._id" : id},
{"items.$.subitems": {$push: { _id : 1, name : "MySubitemName" }}});
但它不工作,数据库看起来像以前一样
有人有想法吗?在定义文档结构时,建议您做一件事: 避免在您自己的模式中使用
\u id
,因为此名称是mongodb的内部名称,用于表示ObjectId。如果严格要求您这样做,请使用id
。请看
因此,您的文档现在可以是:
{
"id" : "f9d01fd1ef22684353149851",
"name" : "Ueberschrift",
"items" : [
{
"id" : "be695ec7ffe71152088c57e1",
"name" : "iop",
"checked" : false,
"subitems" : [ ]
},
{
"id" : "fe665ec7ffe78852088c22g8",
"name" : "asd",
"checked" : false,
"subitems" : [ ]
}
]
}
更新查询:
Listitems.update({ id:"f9d01fd1ef22684353149851", "items.id" : "be695ec7ffe71152088c57e1"}, {$push: {"items.$.subitems" :{ id : 1, name : "MySubitemName" }}});
我已经使用mongodb控制台进行了测试,我假设更新查询中提供的对象与控制台中的对象匹配
请告诉我们这是否有效
编辑以改进评论:
由于查询时您指的是实际的对象id(\u id
),请查看下面的代码,作为对象引用的可能修复:
uid => ObjectId("f9d01fd1ef22684353149851") and
id => ObjectId("be695ec7ffe71152088c57e1")
Listitems.update({_id:uid, "items._id" : id},
{$push: {"items.$.subitems" :{ id : 1, name : "MySubitemName" }}});
问题可能是minimongo还不支持mongo的“$”占位符操作符。如果是这种情况,从服务器调用更新时,您的更新可能运行正常,但从客户端调用时,更新可能会失败。您当前从何处调用该更新 有关Meteor团队提供的解决方案示例,请参见其“parties”应用程序的model.js: 我已经在下面粘贴了关键行144-156,包括它们的注释——简言之,它们在服务器上使用“$”并在客户端上用数组索引替换它
if (Meteor.isServer) {
// update the appropriate rsvp entry with $
Parties.update(
{_id: partyId, "rsvps.user": this.userId},
{$set: {"rsvps.$.rsvp": rsvp}});
} else {
// minimongo doesn't yet support $ in modifier. as a temporary
// workaround, make a modifier that uses an index. this is
// safe on the client since there's only one thread.
var modifier = {$set: {}};
modifier.$set["rsvps." + rsvpIndex + ".rsvp"] = rsvp;
Parties.update(partyId, modifier);
}
我认为语法应该是
{“$push”:{“items.$.subitems”:{/*doc*/}}
。另外请注意“
周围的$push
@ExplosionPills谢谢您的回答,但它也不起作用,。。。我已将命令更改为Listitems.update({u-id:uid,“items.\u-id”:id},{“$push”:{“items.$.subitems”:{{u-id:ue-id,name:text}});但我得到的唯一结果是:>db.listitems.find(){“_id”:“f9d01fd1ef22684353149851”,“name”:“Ueberschrift”,“items”:[{“_id”:“be695ec7ffe71152088c57e1”,“name”:“asd”,“checked”:false,“subitems”:[]}}}谢谢你的回答,但它不起作用,我没有任何错误,但是数据集没有更新您是否使用更改的模式进行了测试(使用id而不是_id)?这是我第一次使用您的原始代码进行测试时遇到的问题。不,我没有更改我方案中的_id。我使用var id=new Meteor.Collection.ObjectID().valueOf()生成它;这不对吗?或者mongodb使用的是不可见的内部id方案?您可以使用类似{“_id”:objectId(“f9d01fd1ef22684353149851”)}的内容搜索mongodb的objectId。试试这个:Listitems.update({“\u id”:ObjectId(“f9d01fd1ef22684353149851”)},{$push:{“items.$.subitems:{id:1,name:“MySubitemName”}});请在帖子中找到改进后的答案。也许这可以让你的查询正常工作。我已经在服务器端使用了db命令,但它也不起作用