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
Related
This question already has answers here:
How to remove array element in mongodb?
(6 answers)
Closed 2 years ago.
I have been searching all day to figure out how to do a simple delete for one element inside a document that is inside a collection. I am fairly new to mongo scripts so bear with me.
I am currently using this script to see a certain document:
db.getCollection('roles').deleteOne({"_id": "e9b4afad-cb5c-4588-9560-161867faa5b7"})
This is what the document shows in Robo3T after the it is executed:
{
"_id" : "e9b4afad-cb5c-4588-9560-161867faa5b7",
"appIds" : [
"rpt",
"wc",
"bw"
],
}
I am simply wanting to remove "rpt" from appIds. I have tried this so far and it doesn't work:
db.getCollection('roles').find({"_id": "e9b4afad-cb5c-4588-9560-161867faa5b7"}).remove({"appIds": "rpt"})
Any documentation that'll point me in the right direction?
Thanks!
It is not an easy task but I believe $pull will do the work.
The $pull operator removes from an existing array (in a doc) all instances of a value or values that match a specified condition.
I would try,
db.roles.update(
{ },
{ $pull: { appIds: "rpt"}},
{ multi: true }
)
Let me know if it works...
If you want to update a specific id just add it in the first part {}, -> {_id: *****},
See the great documentation of MongoDB at https://docs.mongodb.com/manual/reference/operator/update/pull/
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);
})
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 } } );
I need to search for the existence of a property that is within another object.
the collection contains documents that look like:
"properties": {
"source": {
"a/name": 12837,
"a/different/name": 76129
}
}
As you can see below, part of the query string is from a variable.
With some help from JohnnyHK (see mongo query - does property exist? for more info), I've got a query that works by doing the following:
var name = 'a/name';
var query = {};
query['properties.source.' + name] = {$exists: true};
collection.find(query).toArray(function...
Now I need to see if I can index the collection to improve the performance of this query.
I don't have a clue how to do this or if it is even possible to index for this.
Suggestions?
2 things happening in here.
First probably you are looking for sparse indexes.
http://docs.mongodb.org/manual/core/index-sparse/
In your case it could be a sparse index on "properties.source.a/name" field. Making indexes on field will dramatically improve your query lookup time.
db.yourCollectionName.createIndex( { "properties.source.a/name": 1 }, { sparse: true } )
Second thing. Always when you want to know whether your query is fast/slow, use mongo console, run your query and on its result call explain method.
db.yourCollectionName.find(query).explain();
Thanks to it you will know whether your query uses indexes or not, how many documents it had to check in order to complete query and some others useful information.
Basically the question is simple:
How can I issue a query on a collection, which starts with _?
For example if I have 2 collections test and _test, and I am trying db.test.findOne() and db._test.findOne() in mongoshell the first one is working as intended, whereas the second tells me TypeError: db._testhas no properties (shell):1
Place it in quotes and use the getCollection method. See this article
Example To create a collection _foo and insert the { a : 1 } document, use the following operation:
db.getCollection("_foo").insert( { a : 1 } )
To perform a query, use the find() method, in as the following:
db.getCollection("_foo").find()