Elie Asmar
Elie Asmar

Reputation: 3165

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.

Upvotes: 7

Views: 19579

Answers (1)

Rijad Rahim
Rijad Rahim

Reputation: 419

Replace the {query} with query.

var cursor = db.collection("tbl_Patients").find(query);

Upvotes: 10

Related Questions