我希望将$push
内容放入嵌套数组中,其中父数组匹配一个简单的属性条件:
以下是我的文档的外观:
{
name: "Foo",
boardBucket: {
currentBoardId: 1234,
items: [ <- looking to push into `boardItems` of an `item` in this Array
{
boardId: 1234, <- that has `boardId: 1234`
boardItems: [ "barItem", "deyItem" ] <- Final Array I want to push to
}
]
}
}
所以我想在"fooItem"
boardItems
item
boardId: 1234
推送$push
选项1:我可以使用点表示法和按索引访问
我当然可以使用dot.notation执行this.update({ '$push': {"boardBucket.items.0.boardItems": "fooItem" } });
,它使用项目的索引,如下所示:
boardItems
但如果我不知道索引怎么办?
如何使用item
使用boardId: 1234
使用索引(使用boardId
而不是)进入save()
update()
个 object passingmapstomethod {
def main(args:Array[String])={
val custs = Map(100->"surender",101->"raja")
val txns = Map(100->"USA", 101->"UK")
println(join(custs,txns))
}
def join[K,A,B,C](custs:Map[K,A],txns:Map[K,B]) :Map[K, C]=
{
for((k,va) <- custs; vb <- txns.get(k)) yield k -> va.toString()+"|"+ vb.toString()
}
}
?
注意:
Map(101 -> raja|UK , 100 -> surender|USA)
因为它往往是错误的+它似乎在本地保留了一个我想避免的对象副本◾Implicit conversion found: ⇒ any2stringadd(): any2stringadd[(K, String)]
◾type mismatch; found : scala.collection.mutable.Iterable[String] required: scala.collection.mutable.Map[K,C]
◾Implicit conversion found: ⇒ option2Iterable(): Iterable[String]
◾Implicit conversion found: ⇒ ArrowAssoc(): ArrowAssoc[K]
mongo查询就是我追求的目标答案 0 :(得分:2)
(对不起首先没有抽样,然后匆匆忙忙)
db.myDb.insert({
name: "Foo",
boardBucket: {
currentBoardId: 1234,
items: [
{
boardId: 1234,
boardItems: [ "barItem", "deyItem" ]
},
{
boardId: 1235,
boardItems: [ "dontPushToThisOne" ]
}
]
}
});
db.myDb.insert({
name: "Foo2",
boardBucket: {
currentBoardId: 1236,
items: [
{
boardId: 1236,
boardItems: [ "dontPushToThisOne" ]
}
]
}
});
db.myDb.update(
{ "boardBucket.currentBoardId":1234,
"boardBucket.items.boardId":1234},
{ "$push" : {"boardBucket.items.$.boardItems":"fooItem"} }, {multi:1} );
答案 1 :(得分:2)
我认为这应该可以解决问题:
this.update(
{"boardBucket.items": {$elemMatch: { boardId: "1234"}}},
{'$push': {"boardBucket.items.boardItems": "fooItem" }}
);