Reputation: 101
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})
Upvotes: 0
Views: 411
Reputation: 10737
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?} ]
Upvotes: 1