This question already has answers here:
MongoDB Query Help - query on values of any key in a sub-object
(3 answers)
Closed 8 years ago.
I'm asking this question after studying MongoDB text search and keyword indexes. I also found a similar question, but adding an array to each document containing all the field values seems like a kludgy solution. Thus this question.
We need to write a query that will return documents where any field matches a search string.
Our first thought was to write a query along these lines:
db.collection.find( { field_one: /search string/i,
field_two: /search string/i,
field_three: /search string/i } );
Our challenge is that the fields will vary between documents, so we can't hard-code the field names in the query.
Is something like the following pseudocode possible with MongoDB?
db.collection.find( { [any field]: /search string/i } );
Maybe it's not exactly what you are looking for, but if you are considering a schema restructure that allows such a query, this post appears to be interesting:
MongoDB Query Help - query on values of any key in a sub-object
Related
This question already has answers here:
How to limit number of updating documents in mongodb
(8 answers)
Closed 5 years ago.
Lets say I have a 10 documents of Item in the database.
Lets retrieve 3 documents of Item matching some condition using limit().
documents = Item.objects(somefield=somecondition).limit(3)
Now if I do
documents.update(), mongoengine updates all the documents in the database matched by the query not just the 3 documents I have limited my query to.
I also tried setting multi=False in the params, but then only one document gets updated.
Is there anyway to do update while querying itself instead of looping over the documents one by one?
As far as I know there is no available solution to your problem provided by MongoDB. However you could try something like this
documents.forEach(
function (e) {
e.field = 'value';
db.collection.save(e);
}
);
This question already has answers here:
In MongoDB search in an array and sort by number of matches
(2 answers)
Closed 7 years ago.
I have a mongo collection that has docs similar to the schema below. And I have a ui from which a user could filter on the attributes (using logical or). Since its using OR I want the results to be ordered such that I first get the ones that match the most filters. For example: if i filter on author "auth1" and "tag1" I get back both records but the second record is on top. In this example I just have 2 attr to filter on, but there are about 20.
Do you have any suggestions of the best way of tackling this? I was thinking of writing a map-reduce query to compute the "match rank".
{name: "bookName", tags:["tag1"], authors: [] }
{name: "bookName2", tags:["tag1", "tag2"], authors: ["auth1", "auth2"] }
If I understand your proble, You wants sort the result By authors.length and tags.length.
The problem, in MongoDB (I test on 2.6) with the sort(), it is impossible to sort by two parallels arrays. You can sort by album.length or tags.length but not both.
// sort By max to min authors
db.getCollection('rank').find({}).sort({ authors : -1 });
// sort by max to min tags
db.getCollection('rank').find({}).sort({ tags : -1 });
If you watn sort your result by both of them, you should use the Aggregation Framework. You have a great explanation here : https://stackoverflow.com/a/9040269/1506914
It is possible using Aggregation Framework. Have a look at this question, it's similar to yours.
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.
This question already has answers here:
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 9 years ago.
Consider my query to be: {cheese:"Cheddar"} and I have the following collections:
{vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}
How do I make a find that returns me all collections that include cheese:Cheddar?
The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.
I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.
I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.
Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Full-text search in NoSQL databases
I am somewhat new to database querying and i was wondering what the best way to do this would be. I have a database of articles and want my users to be able to search them by keywords in the tittle, i.e allowing them to type a string and all the tittles containing this string would be selected by the query.
What would the most efficient way to do this be?
And if i want to avoid strings such as "the" or "it" from being selected?
I am using mongoid in case that helps.
Thanks in advance
If your title is stored as a string you could use the regular expression search supported by mongodb. For example:
db.articles.find( { title : /acme.*corp/i } );
Mongodb use PCRE for regular expression. To exclude certain words from the search I would recommend an application side check or you can use the $nin operator. For more info have a look here.