This question already has answers here:
How to query MongoDB with "like"
(45 answers)
MongoDB: Is it possible to make a case-insensitive query?
(27 answers)
Closed 4 years ago.
I'd like to search my database for Customers that start with FOO or have FOO in their names. For that i took a look at this question -> How to query MongoDB with "like"?
After that i build my query so that it looks like this ->
Customer.find({'name': '/FOO/'}).exec(function (err, customer) {
console.log(customer);
})
but although there is a customer with FOO inside the 'name' i get no result. If i modify my query to ...({'name': 'FOO'})... instead of ...({'name': '/FOO/'})... i get my customer. What am i doing wrong?
Try to use $regex with $options
Customer.find({'name': { $regex: 'FOO', $options: 'i' }}).exec(function (err, customer) {
console.log(customer);
})
Related
This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 2 years ago.
I am trying to query a collection with a like query, which we do in relational DB. I am trying to get a config which ends with LOYALTY_SERVICE_ENABLED
However, I couldn't use like query as I wanted. Can you explain the difference between the two queries below? Thanks
RETURNS 1 RESULT
db.getCollection("configurations").find(
{ key: 'co:food:LOYALTY_SERVICE_ENABLED' }
);
RETURNS 0 RESULT
db.getCollection("configurations").find(
{ key: '/.*LOYALTY_SERVICE_ENABLED' }
);
Equal SQL Query:Select * from configurations where key like '%LOYALTY_SERVICE_ENABLED';
It looks like a typo. can you try it like this?
db.getCollection(“configurations”).find({"key" : /.*LOYALTY_SERVICE_ENABLED.*/}).pretty();
You will need to use the $regex evaluation query operator to use a pattern matching string, I have modified your code to reflect that.
db.getCollection("configurations").find(
{ key: {$regex: /.*LOYALTY_SERVICE_ENABLED/} }
);
Notice you don't need the quotes ' ' around the pattern string.
More details can be found here
This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 3 years ago.
I tried to get embedded documents in the table using mongodb but its not working.
dev table
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":false},{"id":1,"name":true}]
}]
Query
db.dev,find({data.name:true})
Excepted output
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true}]
}]
I got Output
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":"false"},{"id":1,"name":true}]
}]
How to write the query to match the expected output. can give sample code
Try out this solution
let whereClause = { "data.name":true };
await db.dev.find(whereClause);
This question already has answers here:
How do I perform the SQL Join equivalent in MongoDB?
(19 answers)
MongoDB Aggregate lookup in Go (mgo.v2)
(1 answer)
Closed 4 years ago.
I have two collections.
I am using MongoDB version 3.4.
Collection 1 called Results
Collection 2 called Code_Value
Here i have to update automatically the code in the result using the Code_value Collections
The results is like:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"3"
salary:"4"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"1"
salary:"2"
}
the Code_value is like:
{
"Job":[
{"code":"1" , "value":"IT"}
{"code":"2" , "value":"Doctor"}
{"code":"3" , "value":"Developer"}
]
"Salary":[
{"code":"1" , "value":"900000"}
{"code":"2" , "value":"100000"}
{"code":"3" , "value":"200000"}
{"code":"4" , "value":"300000"}
]
}
I'd like to have a mapping code value on the results as the following:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"Developer"
salary:"300000"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"IT"
salary:"100000"
}
here is the query made as function
results.find().foreach(cursor){
v=Code_value.find(cursor,code)
if(v){
results.update(job,v.value)
}
This question already has answers here:
MongoDB: How to update multiple documents with a single command?
(13 answers)
FindAndModify, return array of Objects
(1 answer)
Closed 5 years ago.
I am trying to use mongoose's findByIdAndUpdate method to pass in a list of object id's and updating them at once. However, I am getting a "Error: Can't use $set with ObjectId." error which I can't seem to associate with my code.
Here's my code.
return ComponentModel.findByIdAndUpdate({
_id: {
$in: input.subjectIds
},
$set: { location: input.newLocation }
}).then(res => res);
findByIdAndUpdate is for one document. For multiple documents you can use update with multi flag true.
return ComponentModel.update(
{_id: {$in: input.subjectIds}},
{$set: {location: input.newLocation}},
{"multi": true}
).then(res => res);
This question already has answers here:
Mongodb Query To select records having a given key
(3 answers)
Closed 7 years ago.
I have documents stored in mongo database following this schema:
{
map:{
key1:value,
banana:value2
....
}
}
How can I query objects based on keys in this map ?
e.g I want to get all the documents which map contains key that equals banana.
Maps are accessed the same way as normal nested values.
This means that you can use the $exists operator to check if the key exists.
db.collection.find( { "map.banana" : { $exists : true } } );