TiagoM
TiagoM

Reputation: 3526

Finding all records containing any kind of subfield in mongodb

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

Upvotes: 0

Views: 59

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

It's simply:

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

You were very close!

Upvotes: 1

Related Questions