mongodb查询中的ServletException

时间:2016-07-10 15:48:59

标签: json mongodb mongodb-query mongodb-java

{
   id:1,  list:{
     time:9:00AM,
     branch:[cse,it],
     count:30,
   },  id:2,  list:{
     time:9:30AM,
     branch:[cse,it],
     count:20,
   }
}

以下代码是根据json文档中的id和time追加新分支

public DBObject appendRoute(String id, String time, String groups,
    int count)  {
        DBCollection coll = db.getCollection("groups");
        BasicDBObject queryDocument = new BasicDBObject("id", id)
            .append("list.time", time).append("list.count", count);
        List<DBObject> sourceDocument = coll.find(queryDocument).toArray();
        BasicDBObject elementToArray = new BasicDBObject("list.branch",
            branch);
        BasicDBObject pushElement = new BasicDBObject("$push", elementToArray);
        coll.update(queryDocument, pushElement);

        System.out.println(sourceDocument);
        return sourceDocument.get(0);
    }

此代码抛出以下错误:

javax.servlet.ServletException: com.mongodb.WriteConcernException: {
"serverUsed" : "localhost/127.0.0.1:27017" , "connectionId" : 1 ,
"err" : "cannot use the part (list of list.count) to traverse the
element ({list: [ { time: \"9:00AM\", branches: [ ece ], count: \"20\"
}, { time: \"9:05AM\", branch: [ cse ], count: \"20\" },  "code" :
16837 , "n" : 0 , "ok" : 1.0}
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

请告诉我代码中的问题是什么?

1 个答案:

答案 0 :(得分:0)

您的Mongo查询似乎是正确的。如果您使用的是最新版本的Java驱动程序,请使用以下代码。它应该可以解决您的问题。

我没有你的Java驱动程序版本。所以我无法复制这个问题。

Mongo查询:

db.groups.updateOne({"_id" : ObjectId("5782abde4e0f937abf0fcbd3"), 
    "list.time" : "9:00AM" , "list.count" : 30}, {"$push" : {"list.branch" : "ece"}});

我使用最新的Java驱动程序编写了您的解决方案。它工作正常。

<dependency>        <groupId>org.mongodb</groupId>      <artifactId>mongo-java-driver</artifactId>      <version>3.2.2</version>    </dependency>

<强>代码: -

以下代码已经过测试并且运行正常。

import org.bson.Document; import org.bson.types.ObjectId;

import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; import com.mongodb.client.result.UpdateResult;

public class MongoGroupsExample {

    public static void main(String[] args) {        MongoClient client = new MongoClient();         MongoDatabase database = client.getDatabase("test");

        int count = 30;

        Document findDoc = database.getCollection("groups")
                .find(Filters.and(Filters.eq("_id", new ObjectId("5782abde4e0f937abf0fcbd3")),
                        Filters.eq("list.time", "9:00AM"), Filters.eq("list.count", count)))
                .first();

        System.out.println(findDoc.toJson());

        UpdateResult updateResults = database.getCollection("groups").updateOne(
                Filters.and(Filters.eq("_id", new ObjectId("5782abde4e0f937abf0fcbd3")),
                        Filters.eq("list.time", "9:00AM"), Filters.eq("list.count", count)),
                Updates.push("list.branch", "ece"));

        System.out.println(updateResults.getModifiedCount());

    }

}