Home > Back-end >  how to write $or query in node
how to write $or query in node

Time:02-09

This is how I write Mongo queries in Node.

query["serviceProvider.type"] = "prepaid";

but I need to write this query in my Node code which works only in MongoDB CLI. How can I achieve this i.e. I need to query same keys with two different values.

db = db.getSiblingDB("test");
db.getCollection("transactions").find(
    { 
        
            
                $or : [
                    { 
                        "serviceProvider.type" : "prepaid"
                    }, 
                    { 
                        "serviceProvider.type" : "dth"
                    }
                ]
            
    }
);

{ 
    "_id" : ObjectId("5d8b057e3a22472facdb89a4"), 
    "customer" : {
        "isCustomerPaid" : true, 
        "customerID" : "", 
        "customer" : ""
    }, 
    "transaction" : {
        "transStatus" : false, 
        "transID" : "15693919985540280", 
        "number" : "8332900803", 
        "amount" : NumberInt(5), 
        "error" : "Something went wrong. Please try again after sometime."
    }, 
    "couponSurvey" : {
        "status" : false, 
        "isEligible" : false
    }, 
    "status" : true, 
    "advisor" : {
        "advisorID" : "BAA0280", 
        "name" : "John BA", 
        "phoneNumber" : 8005301205.0
    }, 
    "serviceProvider" : {
        "type" : "dth", 
        "providerID" : "5bbb42e7a28b1daee826d7ec", 
        "providerName" : "Sun Direct"
    }, 
    "provider" : {
        "refId" : "00", 
        "status" : "fail", 
        "startTime" : ISODate("2019-09-25T06:13:18.554 0000"), 
        "endTime" : ISODate("2019-09-25T06:13:18.554 0000"), 
        "message" : "Invalid Denomination"
    }, 
    "extras" : {
        "imei" : "865300302225654", 
        "latitude" : "99.9385189", 
        "longitude" : "11.5827393"
    }, 
    "createdAt" : ISODate("2019-09-25T06:13:18.790 0000"), 
    "updatedAt" : ISODate("2019-09-25T06:13:18.790 0000"), 
    "__v" : NumberInt(0)
}

This is the sample data in database. Please let me know how I can query the parameters I require.

CodePudding user response:

One thing, your query has a type, there is an space after the second serviceProvider so the value dth is not found.

Also, to create an $or query you can simply use the same style:

let findQuery = {}
const q1 = {"serviceProvider.type" : "prepaid"}
const q2 = {"serviceProvider. type" : "dth"}
findQuery['$or'] = [q1,q2]

console.log(findQuery)

Or even if you have your values in an array:

const values = ["prepaid","dth"]
let findQuery = {
'$or': values.map(m => {return {"serviceProvider.type":m}} )
}

console.log(findQuery)

Or, even... you can use $regex to find into that two values:

const values = ["prepaid","dth"]
let findQuery = {
  "serviceProvider.type":{
    '$regex':values.join('|')
  }
}

console.log(findQuery)

Example here

CodePudding user response:

If you want to query if value at a particular key is one of a list of values, instead of bothering with $or you can use $in.

const findQuery = {
  "serviceProvider.type": {
    $in: [
      "prepaid",
      "dth"
    ]
  }
}

Mongoplayground Link Here

  •  Tags:  
  • Related