1

Below is the schema of document in 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)
        }
    ]
}

I want to find all documents with a specific tag for example only by oHE or oHE,SJx . I am not able to find correct query .

db.poss.find({ Tags: { $in: { [ "oHE","SJx" ] } }})
2
  • 3
    @Rudra Comments like that are the reason I personally wish that comments should be able to be downvoted. Completely incorrect. Commented Aug 23, 2015 at 12:04
  • Also since all of your dates here work out to be the "1st January 01" ( as in year 1 A.D ) then I suggest you have quite a few problems here as well as the fact that your "array" only contains a singular string and not an array of strings, as you seem to expect it should. Commented Aug 23, 2015 at 12:08

3 Answers 3

3

You stored tags as single array item ["oHE,SJx,FMQ"] instead of multiple items ["oHE", "SJx, "FMQ"] that's why query doesn't work.

You can convert tags string to array using code bellow:

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

or

db.poss.find().forEach(function (el) {
    var tags = el.Tags[0];
    el.Tags = tags.split(',');
    db.poss.save(el);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Show them how to correct the data and it's a vote from me.
Not "optimal", but certainly better than other responses here. Learn about Bulk operations, and also why update operators are preferred over .save(). But enough to get the OP here on the correct track.
Thanks for quick information it helped . The problem was with my class design .
0

Firs of all - make it ["oHE", "SJx, "FMQ"] instead of ["oHE,SJx,FMQ"]. Then change your query to db.poss.find({Tags: {$in: ["oHE", "SJx"] }})

1 Comment

Thanks for quick information it helped . The problem was with my class design .
0

The best way is using Bulk operations

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()

Then your data look like this:

{
        "_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)"
                }
        ]
}

Now your query:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.