我有一个Mongo DB Collection:
每个日期(例如2013-03-16)可以有3个国家/地区值:IND,US和SA,如下所示:
{
"name": "SAM",
"collection": [
{
"date": "2013-03-16",
"country": "IND",
"values": [
{
"price": "24.0",
"gece": "342.1",
"countrycode": "IN"
},
{
"price": "24.0",
"gece": "322.1",
"countrycode": "IN"
}
]
},
{
"date": "2013-03-16",
"country": "US",
"values": [
{
"price": "10.0",
"gece": "342.1",
"countrycode": "US"
},
{
"price": "120.0",
"gece": "342.1",
"countrycode": "US"
}
]
},
{
"date": "2013-03-16",
"country": "SA",
"values": [
{
"price": "12.0",
"gece": "432.1",
"country": "SA"
}
]
},
{
"date": "2013-03-17",
"country": "IND",
"values": [
{
"price": "10.0",
"gece": "532.1",
"country": "IN"
}
]
},
{
"date": "2013-03-17",
"country": "US",
"values": [
{
"price": "38.0",
"gece": "332.1",
"country": "US"
}
]
}
]
}
我尝试使用以下代码来测试结构:
BasicDBObject query = new BasicDBObject();
query.put("name", "SAM");
query.put("collection.date", "2013-03-17");
query.put("collection.country", "IND");
BasicDBObject fields = new BasicDBObject();
fields.put("name", 1);
fields.put("collection.$", 1);
我正在查询日期2013-03-17,但它正在返回2013-03-16的数据。
以下是回复:
{
"_id": {
"$oid": "513121ac0651150e227d2224"
},
"name": "SAM",
"collection": [
{
"date": "2013-03-16",
"country": "IND",
"values": [
{
"price": "24.0",
"gece": "342.1",
"countrycode": "IN"
},
{
"price": "24.0",
"gece": "322.1",
"countrycode": "IN"
}
]
}
]
}
为什么在我提供的日期返回错误的数据?
请参阅已编辑的部分
Mongo mongo = new Mongo();
DB db = mongo.getDB("test");
DBCollection mycollection = db.getCollection("xxx");
BasicDBObject query = new BasicDBObject();
query.put("name", "SAM");
query.put("collection.date", "2013-03-16");
query.put("collection.country", "IND");
BasicDBObject fields = new BasicDBObject();
fields.put("name", 1);
fields.put("collection.$", 1);
BasicDBObject subquery = new BasicDBObject();
subquery.put("date", "2013-03-16");
subquery.put("country", "IND");
query.put("collection", new BasicDBObject("$elemMatch", subquery));
DBCursor cursor = mycollection.find(query);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
我仍然得到了所有结果
{ "_id" : { "$oid" : "513121ac0651150e227d2224"} , "name" : "SAM" , "collection" : [ { "date" : "2013-03-16" , "country" : "IND" , "values" : [ { "price" : "24.0" , "gece" : "342.1" , "countrycode" : "IN"} , { "price" : "24.0" , "gece" : "322.1" , "countrycode" : "IN"}]} , { "date" : "2013-03-16" , "country" : "US" , "values" : [ { "price" : "10.0" , "gece" : "342.1" , "countrycode" : "US"} , { "price" : "120.0" , "gece" : "342.1" , "countrycode" : "US"}]} , { "date" : "2013-03-16" , "country" : "SA" , "values" : [ { "price" : "12.0" , "gece" : "432.1" , "country" : "SA"}]} , { "date" : "2013-03-17" , "country" : "IND" , "values" : [ { "price" : "10.0" , "gece" : "532.1" , "country" : "IN"}]} , { "date" : "2013-03-17" , "country" : "US" , "values" : [ { "price" : "38.0" , "gece" : "332.1" , "country" : "US"}]}]}
答案 0 :(得分:3)
如果您要使用positional $ operator,则只允许您的查询包含一个数组字段。
您的查询应该是这样的:
BasicDBObject subquery = new BasicDBObject();
subquery.put("date", "2013-03-17");
subquery.put("country", "IND");
query.put("collection", new BasicDBObject("$elemMatch", subquery));