Mongodb $查询数组无法正常工作

时间:2015-08-23 11:55:33

标签: mongodb mongodb-query

以下是mongo db中的文档架构

{
    "_id" : ObjectId("55d9a2f467d16f15a886a532"),
    "Author" : "RYTnirY",
    "Title" : "MZDCGMyXLV",
    "Content" : "HotEXFcyjaipabbAXAkKR",
    "Tags" : [ 
        "oHE,SJx,FMQ"
    ],
    "CreatedAtUtc" : ISODate("2015-08-23T10:39:48.766Z"),
    "Comments" : [ 
        {
            "Author" : "RWfSxDZ",
            "Content" : "TvYfJzLtIeaIdrxdsbQ",
            "CreatedAtUtc" : Date(-62135596800000)
        }, 
        {
            "Author" : "RFmUqfD",
            "Content" : "lHpUwrLnzXMSFtpGmo",
            "CreatedAtUtc" : Date(-62135596800000)
        }
    ]
}

我想查找具有特定标签的所有文档,例如仅通过oHE或oHE,SJx。我无法找到正确的查询。

db.poss.find({ Tags: { $in: { [ "oHE","SJx" ] } }})

3 个答案:

答案 0 :(得分:2)

您将代码存储为单个数组try: a = data['foo'] except KeyError: pass try: b = data['bar'] except KeyError: pass try: c = data['fizz'] except KeyError: pass try: d = data['buzz'] except KeyError: pass 而不是多个["oHE,SJx,FMQ"]项,这就是查询无效的原因。

您可以使用以下代码将标记字符串转换为数组:

["oHE", "SJx, "FMQ"]

var cursor = db.poss.find();
while (cursor.hasNext()) {
  var x = cursor.next();
  var tags = x['Tags'][0];
  print("Before: "+x['Tags']);
  x['Tags'] = tags.split(',');
  print("After: "+x['Tags']);
  db.poss.update({_id : x._id}, x);
}

答案 1 :(得分:0)

首先 - 将其设为["oHE", "SJx, "FMQ"]而不是["oHE,SJx,FMQ"]。 然后将您的查询更改为db.poss.find({Tags: {$in: ["oHE", "SJx"] }})

答案 2 :(得分:0)

最好的方法是使用Bulk操作

var bulk = db.poss.initializeOrderedBulkOp(),
    count = 0;

db.poss.find().forEach(function(doc){ 
    var tag = doc.Tags[0].split(","); 
    bulk.find({"_id": doc._id}).updateOne({
        "$set": { "Tags": tag }
    }); 
    count++; 
    if (count % 500 == 0){
        // Execute per 500 operations and re-init
        bulk.execute();     
        bulk = db.poss.initializeOrderedBulkOp(); 
    } 
})

// Clean up queues
if ( count % 500 != 0 )
    bulk.execute()

然后您的数据如下所示:

{
        "_id" : ObjectId("55d9a2f467d16f15a886a532"),
        "Author" : "RYTnirY",
        "Title" : "MZDCGMyXLV",
        "Content" : "HotEXFcyjaipabbAXAkKR",
        "Tags" : [
                "oHE",
                "SJx",
                "FMQ"
        ],
        "CreatedAtUtc" : ISODate("2015-08-23T10:39:48.766Z"),
        "Comments" : [
                {
                        "Author" : "RWfSxDZ",
                        "Content" : "TvYfJzLtIeaIdrxdsbQ",
                        "CreatedAtUtc" : "Sun Aug 23 2015 15:22:04 GMT+0300 (MSK)"
                },
                {
                        "Author" : "RFmUqfD",
                        "Content" : "lHpUwrLnzXMSFtpGmo",
                        "CreatedAtUtc" : "Sun Aug 23 2015 15:22:04 GMT+0300 (MSK)"
                }
        ]
}

现在您的查询:

db.poss.find({ "Tags": { "$in": [ "oHE","SJx" ]  }})