1

我是新手MongoDB,我想从数组结构中删除一个元素,如下所示:

{
    "Data" : [
        {           
            "url" : "www.adf.com"
            "type":7
        },
        {

            "url" : "www.pqr.com"
            "type":2
        }
        {           
            "url" : "www.adf.com"
            "type":3
        },
        {

            "url" : "www.pqr.com"
            "type":5
        }
    ],

}

我想删除类型为最低值的 url=www.adf.com ,即在本文档中,我的查询应删除 type=3 并返回如下文档:

{
        "Data" : [
            {           
                "url" : "www.adf.com"
                "type":7
            },
            {

                "url" : "www.pqr.com"
                "type":2
            }

            {

                "url" : "www.pqr.com"
                "type":5
            }
        ],

    }
4

2 回答 2

1

@shakthydoss 显示的查询可以用 java 描述如下:

    MongoClient mongoClient = new MongoClient("SERVER", 27017);
    DB db = mongoClient.getDB("DB_NAME");
    DBCollection coll1 = db.getCollection("COLLECTION_NAME");
    DBObject eleMatch = new BasicDBObject();
    eleMatch.put("url", "www.pqr.com");
    eleMatch.put("type", new BasicDBObject("$lte", 50));
    BasicDBObject up = new BasicDBObject();
    up.put("$elemMatch", eleMatch);
    BasicDBList basicDBList = new BasicDBList();
    basicDBList.add(up);
    DBObject query = new BasicDBObject("Data", new BasicDBObject(" $all", basicDBList));
    coll1.find(query);
于 2013-10-28T12:55:34.453 回答
0

将 $all 与 $elemMatch 一起使用

如果该字段包含文档数组,则可以将 $all 与 $elemMatch 运算符一起使用。

db.inventory.find( {
                 Data: { $all: [
                                { "$elemMatch" : { url : "www.pqr.com": "M", type: { $lte: 50} } },

                              ] }
               } )
于 2013-10-27T06:59:57.720 回答