Answer the question
In order to leave comments, you need to log in
How to query mongodb to filter by field type?
I have a field in my database. The general structure of this field is an Array with objects (a list of dictionaries). But there are also records where this field is just a dictionary. I need to fish out these records. I tried like this
{$and: [{'educations':{$type :'object'}}, {educations: {$ne: {$type: 'array'}}}]}
{educations:'object'}
Answer the question
In order to leave comments, you need to log in
In this case, you need to use the $not operator to negate equality. Sample data:
db.test.insertMany([
{
"field1": "abc",
"field2": [
{"embeddedField": "a"},
{"embeddedField": "b"},
{"embeddedField": "c"}
]
},
{
"field1": "xyz",
"field2": {
"embeddedField1": "x",
"embeddedField2": "y",
"embeddedField3": "z"
}
}
])
db.test.find({"field2": {$not: {$type: "array"}}})
{
"_id": ObjectId("..."),
"field1": "xyz",
"field2": {
"embeddedField1": "a",
"embeddedField2": "b",
"embeddedField3": "c"
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question