Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown operator: $and in MongoDB

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.

like image 343
Elie Asmar Avatar asked Oct 20 '17 08:10

Elie Asmar


People also ask

What are operators in MongoDB?

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.

What is MongoDB elemMatch?

Definition. $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

Is exists MongoDB?

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).


1 Answers

Replace the {query} with query.

var cursor = db.collection("tbl_Patients").find(query);
like image 196
Rijad Rahim Avatar answered Sep 23 '22 07:09

Rijad Rahim