I want to get the field of a MongoDB collection where the field is a substring of a longer string.
Real example:
str = 'forest'
MongoDB Collection:
{id: 1, name: est}
{id: 2, name: ab}
I want to get the first item because est is a substring of forest
One way to do this very efficiently is with MongoDB's full-text search. This requires defining a text index type on that particular field.
If you wanted matches that strictly contained your query, check out the exact-phrase operator.
Related
Like what the title said, I'm looking to find the best way to store an array in mongodb (using mongoose) and retrieve the array at anytime and manipulate it?
I tried storing the array as a string in mongoDB and converting it back to an array when I needed to edit it, but I feel that this way is impractical.
So my question is how can I store the array in mongoDB using the "array" Schema Type and what is the best and most effective way to retrive and edit and restore my array?
For example
eg = [0, 1, 2, 3]
I want to edit the array to this:
eg = [0, 1, 6, 3]
Is there a way i could easily access the array?
Although I am not sure exactly what are you trying to accomplish, I will assume that you wish to replace the 3rd value (index=2) in the array (value 2) with the new value 6.
Let's say that schema looks like:
let arrayTestSchema = mongoose.Schema(
{
name: {type: String, required: true},
values: [Number]
}
)
Insert can be done with:
arrayTest.create({name:"test1", values:[0,1,2,3]})
And the replacement with:
arrayTest.updateOne({name:"test1"}, {$set: {"values.2":6}})
Please note that 2 in values.2 represents the index in the array values.
I want to search list of collections from mongoDB have all the keywords of given string.
For e.g.
I have a collection
{
"id":1
"text":"go for shopping",
"description":"you can visit this branch as well"
}
{
"id":2
"text":"check exiting discount",
"description":"We have various discount options"
}
Now, If I will pass string like "I want to go for shopping" w.r.t. text field in find query of mongoDB. Then I should get first collection as output because text field value "go for shopping" exists in the input string passed in find query.
This can be achieved through $text operator in MongoDB. But you have to createIndex on the "text" field in your database.(or whichever filed you want to be matched, I would suggest you rename it in your db to avoid confusion)
db.yourCollectionName.createIndex({"text":"text"})
The first field here is the "text" field in your database, and the second one is the mongo operator.
Then you can pass any query like,
db.yourCollectionName.find({$text: {$search: "I want to go for shopping"}})
The "$text" here is the mongo operator.
This would return all documents which have any of the keywords above.
Maybe you can read more around this and improvise and modify.
Ref: MongoDb $text
You can do so through regular expression. MongoDb provides the provision of matching strings through regex patterns.
In your case you could do something like:
db.yourCollectionName.find({text:{$regex:"go for shopping" }})
This will return you all the documents having the phrase "go for shopping" in the text field.
Ref: MongoDb Regex
I have two objects:
{
genre: ['music', 'movie']
}
and
{
genre: ['movie', 'music']
}
and my query is:
db.test.find({genre :{ $elemMatch:{ $in : ['movie']}}})
and it only gives me the second object. Why? I want to get all the docs that contain a specific genre in their arrays no matter where in the array. How can I do this?
You need neither $elemMatch nor $in in this case. A simple field:value will match documents where field is an array and any one of the values in that array is value. That means
db.test.find({genre :'movie'});
will suffice.
The array query operators are required in more complex situations.
$in is needed when you have a list of possible values and want documents where any of them is found (so db.test.find({genre : { $in:['movie']} }); would work, but would be needlessly convoluted)
$all works like $in but requires that all provided elements are in the array
$elemMatch is a bit more complex. It is required when you want to use multiple operator-conditions (like $gt or $lt) but want only those documents where one array entry matches all the conditions. Without the $elemMatch operator, you get results where each condition is met by at least one array entry, but not necessarily all by the same entry.
Remember db.test.find() in general returns you cursor object. You can access all doc as follows:
entry = db.test.find({genre :{ $elemMatch:{ $in : ['movie']}}})
for doc in entry:
print(doc)
I have a collection in a mongo store where each element has a name attribute. I've sorted it alphabetically by doing
sort({'name': 1})
but now I would like to find only the elements beginning with some letter, say t. So a desired result might return:
[{name: tam}, {name: tom}, {name: tommy}]
How would I go about constructing a query like this?
You need to use regex. In your case it will look like this:
db.collection.find({
name: /^t/
});
I'm building a web application over Node.js and MongoDB which is based on geolocated points.
The document is something like this:
{ name: ""
keywords: [Array of strings]
location: {lng: double, lat: double }
}
I am wondering how could I use find() to find documents that are near from a coordinate but, in addition, are coincident with any of he keywords in the keywords array.
Imagine that keywords are: ["restaurant", "bar", "coffee"]
I've looked into 2d Index, but the secondary index must be a string. It can't be an array of strings.
The problem is that a document could have more than one keyword (or category) so I can't use a simple string to query them
How would you implement this?
Thanks!
What version of mongo? It looks like this was added in 2.4.0: SERVER-8457