I am using MongoDB nodeJS driver to MongoDB database.
Trying to get single result from the database using collection.findOne()
with a query object.
When it is executed, the script gives me null
response. However if the same is executed from the mongo shell, it gives me a response with one document.
Below is the code sample which is making the query to Database.
construct(venue) {
console.log(venue) /* prints -> I love to sit in A\/C */
let promise = new Promise((resolve, reject)=>{
myCollection.findOne({
"Venue": venue
})
.then( r => {
if(r == null) {
throw new Error("Venue name dose not exits in the response");
} else {
resolve(r.VenueCode)
}
})
}
return promise;
}
venue
is a variable which I got after parsing the query parameter from the request URL.
let venue = req.query.venue;
This seems happens only when there is a '\' character in the venue
variable.
For Ex:
I love to sit in A\/C (dose not work! null result)
I love AC ( works fine)
The same string(with backslash) works inside the mongo shell.
db.myCollection.find({"Venue":"I love to sit in A\/C"}).pretty()
{
"_id" : ObjectId("5babsdf92f2c5082d5f478aebfa"),
"VenueCode" : "ABCD",
"Venue" : "I love to sit in A/C ",
"list" : "9999"
}
Update:
Thanks @TimaGegewepe. Removing the backslah from venue
variable did solved my problem.
However still one thing is unclear - when I Insert the data to the modgoDB through another script, It had back slash in the insert document. Inside the mongo shell when I execute the find() with query containing backslash, without back slash both elides the result, But through my script, I have to remove the backslashes inside the query object.