0

I have this collection in my mongo db, here is the code that I have

> db.getCollection("user1@gmail.com_shareds").find();

{
    "_id":ObjectId("56479a5c37863e6c20000029"),
    "uuid":{
        "otheruuid@gmail.com":{
            "collectionPermission":{
                "buys":{
                    "read":true,
                    "write":true
                },
                "shops":{
                    "read":true,
                    "write":false
                },
                "buyers":{
                    "read":true,
                    "write":true
                }
            }
        }
    }
},
{
    "_id":ObjectId("56479b6637863e6c2000002d"),
    "uuid":{
        "lbottoni@live2.it":{
            "collectionPermission":{
                "buys":{
                    "read":true,
                    "write":true
                },
                "shops":{
                    "read":true,
                    "write":false
                },
                "buyers":{
                    "read":true,
                    "write":true
                }
            }
        }
    }
},
{
    "_id":ObjectId("56479b7437863e6c20000030"),
    "uuid":{
        "lbottoni@live.it":{
            "collectionPermission":{
                "buys":{
                    "read":true,
                    "write":true
                },
                "shops":{
                    "read":true,
                    "write":false
                },
                "buyers":{
                    "read":true,
                    "write":true
                }
            }
        }
    }
}

I want to find the uuid equal to lbottoni@live.it, these my test (failed)

db.getCollection("user1@gmail.com_shareds").find({"uuid":{$eq:"lbottoni@live.it"}});
db.getCollection("user1@gmail.com_shareds").find({uuid:{$eq:"lbottoni@live.it"}});
db.getCollection("user1@gmail.com_shareds").find({"uuid":{"$eq":"lbottoni@live.it"}});
db.getCollection("user1@gmail.com_shareds").find({"uuid":{$eq:lbottoni@live.it}});

and others....

always i have zero result...why?what is the correct find?

Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41
lbottoni
  • 962
  • 2
  • 14
  • 26

1 Answers1

0

As it's a key you are looking for, not a value, you should check it with $exists. You can access subkeys with the dot notation

db.getCollection("user1@gmail.com_shareds").find("field.subfield":{$exists:true});

The document for it is here: https://docs.mongodb.org/manual/reference/operator/query/exists/

You are also going to run into problems with a . in your file name. You can try swapping all . for the unicode equivalent \uff0E as described here: https://docs.mongodb.org/manual/faq/developers/#dollar-sign-operator-escaping and How to use dot in field name?. This will give you the following:

db.getCollection("user1@gmail.com_shareds").find("uuid.lbottoni@live\uff0Eit":{$exists:true});
Community
  • 1
  • 1
Geraint Anderson
  • 3,234
  • 4
  • 28
  • 49
  • > db.getCollection("user1@gmail.com_shareds").find("uuid.lbottoni@live.it":$exists:true); 2015-11-18T11:22:56.285+0100 E QUERY SyntaxError: Unexpected token : > db.getCollection("user1@gmail.com_shareds").find("uuid.lbottoni@live.it":{$exists:true}); 2015-11-18T11:23:13.095+0100 E QUERY SyntaxError: Unexpected token : > db.getCollection("user1@gmail.com_shareds").find({"uuid.lbottoni@live.it":{$exists:true}}); > db.getCollection("user1@gmail.com_shareds").find({"uuid.lbottoni@live.it":{$exists:true}}); always not result – lbottoni Nov 18 '15 at 10:25
  • I think the trouble is with the . in your field name. As it stands MongoDB can't distinguish between the subfield "lbottoni@live2.it" and "it" as a subfield of "lbottoni@live2". I'm not sure if it's possible to escape the "." I think a better data structure would be to store the email as a value rather than a key e.g. {"uuid":{"email":"otheruuid@gmail.com"},"collectionPermission":{....... This is assuming you can chage your data structure though! – Geraint Anderson Nov 18 '15 at 11:12
  • Or see this for details on swapping . for the Unicode equivalent: http://stackoverflow.com/questions/8429318/how-to-use-dot-in-field-name – Geraint Anderson Nov 18 '15 at 11:21
  • Yes @GeraintAnderson, here the name limitation in doc https://docs.mongodb.org/manual/reference/limits/#naming-restrictions **Restrictions on Field Names**: Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $). See Dollar Sign Operator Escaping for an alternate approach. – lbottoni Nov 18 '15 at 13:48