I am still learning mongo queries and need help in fixing below aggregate query.
Requirement: A and B collections are related and has a common property "phone number" and "name". Trying to pull the records from collection A where phone number exists in A but not exist in collection B.
SQL equivalent query if it helps:
select * from A
where col1 is not null and phone_number NOT EXISTS(select 1 from B where A.phone_number = B.phone_number and x='123')
I tried to write below mongo query but its throwing error at $ne
db.A.aggregate([
{ $match: { "dob": { $exists: true } } },
{
$lookup: {
from: "B",
localField: "phone_number",
foreignField: "phone_number",
as: "all_profiles"
}
},
{
$match: {
$and: [
{ "all_profiles.col3": "xxxx" },
{ $ne: [
"all_profiles.phone_number",
"$phone_number"
] }
]
}
},
{
$project: {
first_name: 1, last_name: 1, dob: 1, phone_number: 1
}
}
])
Error :
Mongo Server error (MongoCommandException): Command failed with error 2 (BadValue): 'unknown operator: $ne' on server
The full response is: { "ok" : 0.0, "errmsg" : "unknown operator: $ne", "code" : 2.0, "codeName" : "BadValue" }
Thanks