0
votes

I have a question similar to Finding all records containing a given subfield in mongodb, but where you don't know the subfield name.

Given the following documents:

// Document 1
{
   age: 10,
   name: "andrew",
   meta: {
      meta1: true
   }
}

and

// Document 2
{
   age: 10,
   name: "andrew",
   meta:{
   }
}

I want a query that will find documents that have a value defined for any property inside the meta field. In this case, such a query would only match Document 1.

I tried the following:

db.col.find({ meta: { $ne: "" } }) 

But it matched all documents including ones where meta had no subfields.

I only want documents with something inside meta.

I've been struggling searching and trying, but nothing.

Thanks

1
Have you tried db.col.find({meta: {$ne:{}}})?JohnnyHK
wow right... feeling so dumb right now -.- write as an answer please, i would be grateful to accept it, thanks man!TiagoM

1 Answers

1
votes

It's simply:

db.col.find({meta: {$ne:{}}})

You were very close!