mongodb $in limit [duplicate] - mongodb

This question already has answers here:
What is the maximum number of parameters passed to $in query in MongoDB?
(4 answers)
Closed 6 years ago.
Was just wondering if there is a limit to Mongodb's $in function?
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
I have a collection of users (BIG) and have a smaller subset of ObjectIds stashed somewhere, and I want to select all users (collections) that are in my ObjectIds.
Thanks

Since there's no limit on the number of items in an array as such, you shouldn't have any problem..
For the case when the array is embedded inside a document, you might want to have a look at these:
http://groups.google.com/group/mongodb-user/browse_thread/thread/4a7caeba972aa998?fwc=1
Filtering content based on words
http://groups.google.com/group/mongodb-user/browse_thread/thread/28ae76e5ad5fcfb5

Related

update() function disregards limit() in mongo [duplicate]

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);
}
);

Do unique indexes also increase query efficiency in mongoDB? [duplicate]

This question already has answers here:
Advantage of a unique index in MongoDB
(2 answers)
Closed 7 years ago.
I need to create a unique index on a field in mongoDB in order to prevent duplicates in my collection. I also want to create a single-field index on that same field in order to optimize for queries.
Do I need to create these two different indexes? Or will the unique index be used for queries as well?
Any help is appreciated!
The unique index will be used for queries, so the extra index is unnecessary.
You can test this, by looking at the indexes considered in the output from explain in the shell.

Mongodb - Finding a field value existence in another field [duplicate]

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.

MongoDB $in return in order? [duplicate]

This question already has answers here:
Order of responses to MongoDB $in query? [duplicate]
(6 answers)
Closed 8 years ago.
Let's say I'm asking MongoDB for a query like this:
"someField" : {
"$in" : [9,3,7,1]
}
Will the returned objects be sorted by the order of the $in array?
IOW, when looking at the results, will I see all documents where {"someField" : 9} listed before the documents where {"someField":3 }, and then the 7's and then the 1's?
If not, any tips on how to get that?
Thanks!
Unfortunately, not only will the order of the $in array not affect the order of the results, but there also doesn't appear to be a built-in way to provide a custom sorting function to the sort function. You will mostly likely have to implement your own sorting function after retrieving the results. If you can tell us the language you're using to query MongoDB, we could probably help you with that part (if needed).

How do I make a mongodb find where a field(object) includes all properties from my query (names and values)? [duplicate]

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"}).