使用MongoDB更新数组 [英] Update an array using MongoDB

查看:513
本文介绍了使用MongoDB更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON,如:


{_ id:1,_ class:com.model.Test ,itemList:[
{itemID:1,itemName:Foo,
resources:[{resourceID:1,resourceName: Foo Test1,},{resourceID:2,resourceName:Foo
Test2,}]}]}

{ "_id" : "1", "_class" : "com.model.Test", "itemList" : [ { "itemID" : "1", "itemName" : "Foo", "resources" : [ { "resourceID" : "1", "resourceName" : "Foo Test1", }, { "resourceID" : "2", "resourceName" : "Foo Test2", } ] } ] }

我需要能够更新资源列表。
我已完成以下操作:

I need to be able to update the list of resources. I have done the following:

    BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.put("id", "1");

    BasicDBObject updateCommand = new BasicDBObject();

    List<Resource> resources = populateResources();//Fetch a new list of Resources
    updateCommand.put("$push", new BasicDBObject("resources", resources));


    MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate();
    DBCollection db = mongoOperations.getCollection("myCollection");
    db.save(updateCommand);

我收到以下错误:


java.lang.IllegalArgumentException:存储在数据库中的字段不能以$'$ b开头(坏键:'$ push')

java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$push')

当我使用时:

db.update( updateQuery, updateCommand, true, true );

我收到以下异常:


java.lang.IllegalArgumentException:无法序列化类
com.model.Test

java.lang.IllegalArgumentException: can't serialize class com.model.Test

我试过了:
db.updateMulti(updateQuery,updateCommand);
我没有得到任何例外,也没有任何更新发生在文件中。

I've tried : db.updateMulti(updateQuery, updateCommand); I didn't get neither any exceptions nor any updates takes place into the documents.

所以我在这里缺少什么!!

So what am I missing here !!

推荐答案

save()方法失败,因为它试图将以下文档插入集合中:{$ push:{resources: [资源列表]}},$ push不是有效的密钥名称。

The save() method is failing because it is attempting to insert the following document into the collection: {"$push":{"resources":[a list of resources]}}, and "$push" is not a valid key name.

从您的问题看来,似乎您正在尝试将另一个资源文档添加到嵌入文档列表中,资源,在嵌入文档中匹配{ itemID:1},在itemList中。这是正确的吗?

From your question, it appears as though you are attempting to add another resource document to the list of embedded documents, "resources", inside of the embedded document matching {"itemID" : "1"}, inside of "itemList". Is this correct?

处理嵌入式文档的层次很棘手,但可以这样做:

以下是如何插入以下文档使用JS shell进入资源列表:

Dealing with layers of embedded documents is tricky, but it can be done:
Here is how the following document may be inserted into the "resources" list using the JS shell:

> var docToInsert = { "resourceID" : "3", "resourceName" : "Foo Test3"}
> db.myCollection.update({_id:"1", "itemList.itemID":"1"}, {"$push":{"itemList.$.resources":docToInsert}})
> db.myCollection.find().pretty()
{
    "_class" : "com.model.Test",
    "_id" : "1",
    "itemList" : [
        {
            "itemID" : "1",
            "itemName" : "Foo",
            "resources" : [
                {
                    "resourceID" : "1",
                    "resourceName" : "Foo Test1"
                },
                {
                    "resourceID" : "2",
                    "resourceName" : "Foo Test2"
                },
                {
                    "resourceID" : "3",
                    "resourceName" : "Foo Test3"
                }
            ]
        }
    ]
}
> 

使用$位置运算符更新嵌入文档的文档可在更新中找到文件:
http://www.mongodb.org/显示/ DOCS /更新#更新 - %24positionaloperator

The documentation on using the "$" positional operator to update embedded documents may be found in the "Updating" documentation: http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

$ push修饰符的文档也在更新页面上:
http://www.mongodb.org/display/DOCS/Updating#正在更新 - %24push

The documentation on the "$push" modifier is also on the "Updating" page: http://www.mongodb.org/display/DOCS/Updating#Updating-%24push

从发布的代码中,似乎资源是一个列表。您需要使用的方法是$ pushAll,用于向列表中添加多个值:
http://www.mongodb.org/display/DOCS/Updating#Updating-%24pushAll

From the posted code, it appears as though "resources" is a list. It is possible that the method you need to use is $pushAll, used for adding multiple values to a list: http://www.mongodb.org/display/DOCS/Updating#Updating-%24pushAll

使用Java驱动程序,上面的插入可以这样完成:

Using the Java driver, the above insert may be done like so:

Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB("test");
DBCollection myColl = db.getCollection("myCollection");

BasicDBObject docToInsert = new BasicDBObject("resourceID", "3");
docToInsert.put("resourceName", "Foo Test3");

BasicDBObject updateQuery = new BasicDBObject("_id", "1");
updateQuery.put("itemList.itemID", "1");

BasicDBObject updateCommand = new BasicDBObject("$push", new BasicDBObject("itemList.$.resources", docToInsert));

myColl.update(updateQuery, updateCommand);
System.out.println(myColl.findOne().toString());

以上输出如下:

{ "_class" : "com.model.Test" , "_id" : "1" , "itemList" : [ { "itemID" : "1" , "itemName" : "Foo" , "resources" : [ { "resourceID" : "1" , "resourceName" : "Foo Test1"} , { "resourceID" : "2" , "resourceName" : "Foo Test2"} , { "resourceID" : "3" , "resourceName" : "Foo Test3"}]}]}

希望以上将提高您对使用Java驱动程序更新嵌入式文档如何与Mongo一起工作的理解。我注意到这个问题也与Spring有关(mongoOperations是Spring包中的一个类),我很遗憾不熟悉它。如果您仍然遇到更新问题,那么可能需要另一位熟悉Spring的社区成员也可以提供帮助。

Hopefully, the above will improve your understanding of how updating an embedded document works with Mongo using the Java driver. I notice that this question is also related to Spring ("mongoOperations" is a class from the Spring package), which I am unfortunately not familiar with. If you are still having issues with your update, perhaps another member of the Community who is more familiar with Spring will be able to assist.

这篇关于使用MongoDB更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆