0

I have a rather complex structure on my json and I cannot find how to query it to get the rows I am interested in. Here is a sample of my data:

{
    "_id" : ObjectId("5282bf9ce4b05216ca1b68f8"),
    "authorID" : ObjectId("5282a8c3e4b0d7f4f4d07b9a"),
    "blogID" : "7180831558698033600",
    "blogs" : {
        "$" : {
            "posts" : [
                [
                    {
                        "author" : {
                            "displayName" : "mms",
                            ...
                            ...
                            ...
}}}

So, I am interested in finding all json entries that have the author displayName equal to "mms".

My collection name is bz so, a find all query would be: db.dz.find()

What criteria do I have to put inside the find() to only get json document with author displayName equal to mms?

Any ideas?

Thank you in advance!

5
  • "$" is not a recommended field name. Commented Sep 18, 2014 at 2:01
  • Not just not recommended, it's not allowed or supported. docs.mongodb.org/manual/reference/limits/… Commented Sep 18, 2014 at 2:04
  • @JohnnyHK, Thanks for your information. Now I see that V2.6 rejects it even on update operation. By the way, the manual's description is not precise enough. $ is allowed in field name but not as the first character. Commented Sep 18, 2014 at 2:51
  • Well, this query works for me: db.dz.find({"blogs.$.cat" : "red"}) So, in this case $ works a field name. Commented Sep 19, 2014 at 14:11
  • Your problem will be, that $ is a "placeholder" character in a Mongo query. It means, "this item you've found matching the criteria". This will make your queries confusing when you eventually need one that uses the $ operator/helper. It will not mean what you think it means. Commented Sep 21, 2014 at 13:14

1 Answer 1

1

Suppose you have replaced field name "$" with "dollarSign".

Then db.dz.find({"blogs.dollarSign.posts.author.displayName": "mms"}) will fetch whole documents according to your requirements.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this. It does not throw an exception but it does not bring back any result either. I have not replaces the $ field because as I write in my previous comment this does not cause any problems in my case.

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.