0

I'm looking for documents that have both 'e-campaigns' and 'p-campaigns' arrays that are non-empty:

 db.coll.find( { 'p-campaigns':{$ne:[]} ,  'e-campaigns' : {$ne:[]} }     )

I run the query on each array separately and do find docs with non-empty p-campaigns and e-campaigns, but the combined conditions turns out empty. I know there are documents with both p-campaigns and e-campaigns non-empty. I've tried using $and, with the same result: no docs with both arrays non-empty.

Am I going about this the right way? If not, why? This seems like the logical way to run this query on the conditions above. Thanks.

1
  • 2
    You can test for position 0, i.e.: 'e-campaigns.0':{$exists:true} Commented Sep 2, 2013 at 13:14

2 Answers 2

1

Here is my variant:

> db.a.save({x:[], y:[]})
> db.a.save({x:[1], y:[]})
> db.a.save({x:[], y:[1]})
> db.a.save({x:[1], y:[1]})
> db.a.find()
{ "_id" : ObjectId("52248f5efb41276d1f3e3164"), "x" : [ ], "y" : [ ] }
{ "_id" : ObjectId("52248f61fb41276d1f3e3165"), "x" : [ 1 ], "y" : [ ] }
{ "_id" : ObjectId("52248f66fb41276d1f3e3166"), "x" : [ ], "y" : [ 1 ] }
{ "_id" : ObjectId("52248f69fb41276d1f3e3167"), "x" : [ 1 ], "y" : [ 1 ] }
> db.a.find({ 'x':{$ne:[]} ,  'y' : {$ne:[]} })
{ "_id" : ObjectId("52248f69fb41276d1f3e3167"), "x" : [ 1 ], "y" : [ 1 ] }
Sign up to request clarification or add additional context in comments.

2 Comments

It's comforting to see I do understand the querying, but clearly I'm clobbering records with my updates. Back to the manual... Thanks.
this won't work if the field doesn't exist at all - it only works if it's guaranteed to be exactly an empty array.
0

For empty array in mongoDb filter query:

{field: {$size: 0}} // When find field array size is 0 or empty.

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.