gpt4 book ai didi

java - 使用 Java 修改 MongoDB 数组

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:46 26 4
gpt4 key购买 nike

我是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
}
],

}

最佳答案

@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);

关于java - 使用 Java 修改 MongoDB 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com