在 Java 中使用 $elemMatch 实现 Mongodb 查询
Implementing Mongodb query using $elemMatch in Java
我正在使用 Mongo java 驱动程序从 mongo 集合中检索数据。我试图在 java 中实现以下查询:
Json 是:
{
"_id" : ObjectId("56cd284767c74d3d4dd3ec80"),
"comments" : "Hello",
"statusLog" : [
{
"status" : "Submitted",
"startDate" : ISODate("2015-01-14T05:00:00.000Z"),
"endDate" : ISODate("2016-02-29T21:24:24.740Z")
},
{
"status" : "Active",
"startDate" : ISODate("2016-02-29T21:24:24.740Z")
}
],
"createdDate" : ISODate("2015-01-14T05:00:00.000Z")
}
Mongo 查询:
db.CollectionName.find({},{_id: 0, createdDate:1, "statusLog": {$elemMatch: {"status":"Submitted"}}});
这是我在 java 中编写的查询(mongo java 驱动程序 3.4.2):
BasicDBObject query = new BasicDBObject(new BasicDBObject("statusLog",
new BasicDBObject("$elemMatch", new BasicDBObject("status", "Submitted"))));
运行 java 代码 returns 所有状态日志,而不是我要查找的那个。
非常感谢任何帮助。
您应该使用更新的 Document/MongoCollection
api 并使用辅助方法来准备投影。
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.*;
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("db"); // Get DB
MongoCollection<Document> collection = database.getCollection("collection"); // Get Collection
Bson projection = Projections.fields( excludeId(), include("createdDate"), elemMatch("statusLog", eq("status", "Submitted"))); // Add Projections
FindIterable<Document> iterable = collection.find().projection(projection);
使用旧的 BasicDBObject/DBCollection
api
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("db");
DBCollection collection = database.getCollection("collection");
BasicDBObject projection = new BasicDBObject(new BasicDBObject("statusLog",new BasicDBObject("$elemMatch", new BasicDBObject("status", "Submitted"))));
collection.find(new BasicDBObject(), projection);
elemMatch
应该在 find 方法中而不是在投影 bson 中。例如:
collection.find(elemMatch("statusLog", eq("status", "Submitted"))).projection(projection);
使用多个过滤条件在包含多个对象的文档列表中查找项目:
"array_of_data": {$elemMatch: {"AAA": "xxxx", "BBB": "xxxxx"}}
我正在使用 Mongo java 驱动程序从 mongo 集合中检索数据。我试图在 java 中实现以下查询: Json 是:
{
"_id" : ObjectId("56cd284767c74d3d4dd3ec80"),
"comments" : "Hello",
"statusLog" : [
{
"status" : "Submitted",
"startDate" : ISODate("2015-01-14T05:00:00.000Z"),
"endDate" : ISODate("2016-02-29T21:24:24.740Z")
},
{
"status" : "Active",
"startDate" : ISODate("2016-02-29T21:24:24.740Z")
}
],
"createdDate" : ISODate("2015-01-14T05:00:00.000Z")
}
Mongo 查询:
db.CollectionName.find({},{_id: 0, createdDate:1, "statusLog": {$elemMatch: {"status":"Submitted"}}});
这是我在 java 中编写的查询(mongo java 驱动程序 3.4.2):
BasicDBObject query = new BasicDBObject(new BasicDBObject("statusLog",
new BasicDBObject("$elemMatch", new BasicDBObject("status", "Submitted"))));
运行 java 代码 returns 所有状态日志,而不是我要查找的那个。
非常感谢任何帮助。
您应该使用更新的 Document/MongoCollection
api 并使用辅助方法来准备投影。
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.*;
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("db"); // Get DB
MongoCollection<Document> collection = database.getCollection("collection"); // Get Collection
Bson projection = Projections.fields( excludeId(), include("createdDate"), elemMatch("statusLog", eq("status", "Submitted"))); // Add Projections
FindIterable<Document> iterable = collection.find().projection(projection);
使用旧的 BasicDBObject/DBCollection
api
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("db");
DBCollection collection = database.getCollection("collection");
BasicDBObject projection = new BasicDBObject(new BasicDBObject("statusLog",new BasicDBObject("$elemMatch", new BasicDBObject("status", "Submitted"))));
collection.find(new BasicDBObject(), projection);
elemMatch
应该在 find 方法中而不是在投影 bson 中。例如:
collection.find(elemMatch("statusLog", eq("status", "Submitted"))).projection(projection);
使用多个过滤条件在包含多个对象的文档列表中查找项目:
"array_of_data": {$elemMatch: {"AAA": "xxxx", "BBB": "xxxxx"}}