I am querying mongodb through node.js code. My mongo documents collection (Patients Collection) has the following structure:+
Patient collection
{
"_id" : ObjectId("59e5c28f37ce021e142e7ead"),
"MRN" : "00126389",
"Family_Name" : "Jones",
"First_Name" : "Lydia",
"Father_Name" : "Bob",
"Maiden_Name" : "",
"Mother_Name" : "n/a",
"Spouse_Name" : "",
"Address" : "",
"Telephone_Nbr" : "",
"Patient_Visit" : {
"Department" : "ER",
"Hospital_Status" : "Active",
"Case_Nbr" : "17",
"Admission_Date" : "01/04/2011 12:00:00 AM",
"Admission_Time" : "14:02"
}
}
My query execution code is presented below:
mongoClient.connect(mongoConstr, function(err, db) {
if (err) throw err;
var query = {
$and: [{
"Patient_Visit.Department": "ER"
}, {
$or: [{
"Patient_Visit.Hospital_Status": "Active Left"
}, {
"Patient_Visit.Hospital_Status": "Active"
}]
}]
};
var cursor = db.collection("tbl_Patients").find({
query
});
cursor.forEach(function(doc) {
console.log(JSON.stringify(doc));
}, function(err) {
db.close();
throw (err);
});
});
When the request is executed I get the following error:
MongoError: unknown operator: $and
Any help would be appreciated.
Operators are special symbols or keywords that inform a compiler or an interpreter to carry out mathematical or logical operations. The query operators enhance the functionality of MongoDB by allowing developers to create complex queries to interact with data sets that match their applications.
Definition. $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).
Replace the {query} with query.
var cursor = db.collection("tbl_Patients").find(query);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With