3

I have a Mongo collection with an array property, and the array has "empty" objects in it. What query can I use to target these non-empty arrays that have only {}?

I've tried combinations of $exists and $where to no avail.

Examples of data I need to target:

"arrayProperty" : [ {} ]

and

"arrayProperty" : [ {}, {} ]

EDIT:

Here's an example of the schema:

{
    "_id" : ObjectId("53b1ca583d597ce7cbd54646"),
    "arrayProperty" : [ 
        {
            "serialNumber" : "abc123",
            "rfid" : "xyz098",
            "size" : 95,
            "points" : 50,
            "frequency" : "Every day",
            "dateAssigned" : ISODate("2011-02-10T15:27:39.000Z")
        }
    ]
} 
3
  • Are you looking for docs where ANY property contains an empty document? How about if it is not at root level such as prop1: {prop2: {}} Commented Jul 14, 2014 at 20:23
  • Are you looking to find docs where arrayProperty contains only {} objects? For example, should arrayProperty: [ {}, {a: 1} ] match? Commented Jul 14, 2014 at 20:43
  • I'm looking for root properties such as doc.arrayProperty: [{}]. There are also some like doc.arrayProperty: [{}, {}] Commented Jul 15, 2014 at 1:12

2 Answers 2

7

If you're looking to find docs where arrayProperty contains at least one {} then it's just:

db.collection.find({ arrayProperty: {} })

If you're looking to find docs where arrayProperty only contains either one or two {}:

db.collection.find({ arrayProperty: {$in: [ [{}], [{}, {}] ] } })
Sign up to request clarification or add additional context in comments.

1 Comment

I set up a dedicated test collection, and this worked!. I also tested @Trust's solution on the test collection, and that worked too. I'm going to try to flag both answers as correct...hopefully I can. Thanks.
2

The following may/may-not work...

db.collection.aggregate([
{$unwind: {"$arrayProperty"}},
{$match: {arrayProperty: {} }},
{$group: {_id: "$_id"} }
])

Basically what I'm trying to do is unwind arrayProperty, pull out all documents with an arrayProperty as {}, and group all of the unwinded documents back that have the same _id (or you can replace that with whatever unique key field you use).

Again, I don't know for sure if this will work so try it out and let me know.

7 Comments

Didn't seem to work. I had to remove the { } around "$arrayProperty". Even with that I got.. { "result" : [], "ok" : 1 }
Could you perhaps include the schema of a typical document in your question? In any case, you might want to try experimenting with aggregation and see if you can come up with anything.
I updated the question to include an example of the schema. Thanks.
Hmmm... okay I have to say I'm pretty stumped (sorry). Are you using a driver (eg: the Python driver or the Java driver)? If so, you could potentially just loop through arrayProperty from the driver itself and check if each element is an empty document. Unfortunately I'm not sure how else to do it... perhaps JohnnyHK's answer may work but that assumes that you know the maximum amount of empty docs arrayProperty could hold.
I set up a dedicated test collection, and this worked!. I also tested @JohnnyHK's solution on the test collection, and that worked too. I'm going to try to flag both answers as correct...hopefully I can. Thanks.
|

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.