(MongoDB Java) $push 到数组 [英] (MongoDB Java) $push into array
本文介绍了(MongoDB Java) $push 到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是 mongo 2.2.3 和 java 驱动程序.我的困境,我必须将一个字段和值 $push 到一个数组中,但我似乎无法弄清楚如何做到这一点.我的数据示例:
I'm using mongo 2.2.3 and the java driver. My dilemma, I have to $push a field and value into an array, but I cant seem to figure out how to do this. A sample of my data:
"_id" : 1,
"scores" : [
{
"type" : "homework",
"score" : 78.97979
},
{
"type" : "homework",
"score" : 6.99
},
{
"type" : "quiz",
"score" : 99
}
]
我可以在 shell 中 $push:
I can $push in the shell:
db.collection.update({_id:1},{$push:{scores:{type:"quiz", score:99}}})
但是当我把它翻译成 Java 时,我把自己弄糊涂了,把我的键盘扔到墙上.
but it's when I translate this into java I confuse my self and chuck my keyboard at a wall.
到目前为止我的java代码(不完整和错误):
my java code (incomplete and wrong) so far:
DBObject find = new BasicDBObject("_id", 1);
DBObject push = new BasicDBObject("$push", new BasicDBObject(
"scores", new BasicDBObject()));
推荐答案
DBObject listItem = new BasicDBObject("scores", new BasicDBObject("type","quiz").append("score",99));
DBObject updateQuery = new BasicDBObject("$push", listItem);
myCol.update(findQuery, updateQuery);
这篇关于(MongoDB Java) $push 到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文