How do you only show those that exist?
This is my current code:
db.collection.find({ $expr: {$eq: ["fieldA", "fieldB"]}, {"_id":0, "fieldC":1})
I have been trying things like the below but unsure of the correct syntax
db.collection.find({ $expr: {$eq: {$exists: ["fieldA", "fieldB"]}}, {"_id":0, "fieldC":1})
db.collection.find({ $expr: {$eq: ["fieldA": {$exists: true}, "fieldB": {$exists: true}]}}, {"_id":0, "fieldC":1})
CodePudding user response:
I don't think you need to use $expr
for this query.
db.collection.find({ fieldA: { $exists: true }, fieldB: { $exists: true }, _id: 0, fieldC: 1 })
Reference
$exists - MongoDB Documentation
CodePudding user response:
Maybe you are looking for this:
db.collection.aggregate([
{
$match: {
$and: [
{
"fieldA": {
$exists: true
}
},
{
"fieldB": {
$exists: true
}
},
{
$expr: {
$eq: [
"$fieldA",
"$fieldB"
]
}
}
]
}
}
])
Explained: and:[ {exist(A)} ,{exist(B)} ,{A=B?} ]