0

I have to find all the documents which include "p396:branchCode" as key in MongoDB.Value does not matter,can be anything.I tried using

{"p396:branchCode": new RegExp(".*")}

in MongoVUE but i found nothing. My db is very nested and branchCode has superkey "p396:checkTellersEApproveStatus"

Wardruna
  • 198
  • 4
  • 23

3 Answers3

2

Your key is nested under a super-key, so you need to use the dot-operator:

{"p396:checkTellersEApproveStatus.p396:branchCode": {$exists: true}}

This assumes p396:branchCode is always under p396:checkTellersEApproveStatus. When that's not the case, you have a problem, because MongoDB does not allow to do queries for unknown keys. When the number of possible super-keys is low, you could query for all of them with the $or-operator. When not, then your only option is to refactor your objects to arrays. To give an example, a structure like this:

properties: {
     prop1: "value1",
     prop2: "value2",
     prop3: "value3"
}

would be far easier to query for values under arbitrary keys when made to look like this:

properties: [
     { key: "prop1", value:"value1"} ,
     { key: "prop2", value:"value2"},
     { key: "prop3", value:"value3"}
]

because you could just do db.collection.find({"properties.value":"value2"})

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Number of possible super-keys is not low in my case,it big data project so it is very very big.It is very sad MongoDB has no feature like this – Wardruna Jul 15 '15 at 14:56
  • @BerkKarabacak When your tool is not the right for the job, don't blame the tool. Blame the one who picked it... or learn to use it correctly. – Philipp Jul 22 '15 at 16:39
0

Sounds like you want to use the $exists operator.

{'p396:branchCode': {$exists: true}}

This assumes this query is part of the path:

{ 'p396:checkTellersApproveStatus': {'p396:branchCode': {$exists: true}}}

Which can be shortened to:

{ 'p396:checkTellersApproveStatus.p396:branchCode': {$exists: true}}
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • 1
    You forgot to consider the heavy nesting of the DB. That is the main problem here. – Philipp Jul 15 '15 at 14:41
  • @Philipp Not really. I only modified the part of the query the OP provided. But to make it explicit I have included the path as far as I can tell from the provided screen shot. – Jason Cust Jul 15 '15 at 14:44
0

If you are actually "mixing types then that probably is not a good thing. But if all you care about is that the field $exists then that is the operator to use:

db.collection.find({ 
   "p396:checkTellersApproveStatus.p396:branchCode": { "$exists": true }
})

If the values are actuall "all" numeric and you have an expected "range" then use $gt and $lt operators instead. This allows an "index" on the field to be used. And a "sparse" index where this was not present in all documents would improve performance:

db.collection.find({
    "p396:checkTellersApproveStatus.p396:branchCode": {
        "$gt": 0, "$lt": 99999
    }
})

In all cases, this is the "child" of the parent "p396:checkTellersApproveStatus", so you use "not notation" to acess the full path to the property.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135