0

我有一个 Mongo DB 集合:

每个日期(例如 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"}]}]}
4

1 回答 1

3

如果您想使用位置 $ 运算符,您的查询只允许包含一个数组字段。

您的查询应该类似于以下内容:

BasicDBObject subquery = new BasicDBObject();
subquery.put("date", "2013-03-17");
subquery.put("country", "IND");
query.put("collection", new BasicDBObject("$elemMatch", subquery));
于 2013-03-01T23:57:27.793 回答