(MongoDB Java) $push into array

后端 未结 5 626

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:<

5条回答
  •  没有蜡笔的小新
    2020-11-28 10:16

    Using Jongo, you can do as in the shell:

    db.collection.update({_id:1},{$push:{scores:{type:"quiz", score:99}}})
    

    Becomes in Java:

    collection.update("{_id:1}").with("{$push:{scores:{type:#, score:#}}}", "quiz", 99);
    

    No fancy DBObject needed ;-)

提交回复
热议问题