This question already has answers here:
How to print out more than 20 items (documents) in MongoDB's shell?
(8 answers)
Closed 7 years ago.
db.uafiles.find({"operating_system":"Windows XP"},{"is_pc":"True"})
Currently I have a record of 15000 user agent details on a collection.When I'm trying to query, I got only 20 items from the collection.What query would it make me to list the entire items ?
You need to set the DBQuery.shellBatchSize attribute value in order to change the number of iteration which basically corresponds to the number of returned document. more info here
For example to return the 100 documents use
DBQuery.shellBatchSize = 100
To return all documents that match your query criteria use:
DBQuery.shellBatchSize = db.uafiles.count({ "operating_system": "Windows XP" }, { "is_pc": "True" })
Then:
db.uafiles.find({ "operating_system":"Windows XP" }, { "is_pc": "True" })
But why will you want to do that since typing it returns the next 20 documents if any.
Related
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
MongoDB : querying documents with two equal fields, $match and $eq
(2 answers)
Closed 1 year ago.
how can i in mongoDB check field by another field like that in sql:
SELECT `name`,`surname` FROM `users` where `name`=`surname`
for now i try :
Credentials.findOne({ usersLen: { $lte: '$usersMaxLen' } });
^^^ - here i want access field usersMaxLen from collection
but have error:
CastError: Cast to number failed for value "$usersMaxLen" (type string) at path "usersLen" for model "Credentials"
I have a simple query that I want to run on two different collections (each collection has around 50K records) :
db.collectionA.updateMany({}, { $mul: { score: 0.3 } });
db.collectionB.updateMany({}, { $mul: { score: 0.8 } });
So basically I want to multiply the field score by a certain amount. On collectionA it takes 2-3s and on collectionB it takes ~50s. I noticed that I don't have any index on the field score on collection A (which is a dynamic field, updated very often) and for some reason I have a composed index with this field on collectionB.
My question is simple, why does it take so much time for mongo to execute this query and what can I do to make it faster ?
Thanks
I think you have answered the question yourself: there is an index on that field in the second collection, and on changing the value in a document, that index needs recalculation. You could try to drop that index and re-create it after the update has finished
This question already has answers here:
Find document with array that contains a specific value
(13 answers)
Closed 4 years ago.
I have a MongoDB document as follows:
{
"_id" : ObjectId("5c29f3123d8cf714fd9cdb87"),
"Machine" : "host1",
"Pools" : [
"Pool1",
"Pool2"
]
}
How do I find all the documents that have pool Pool1 in "Pools" key in my collection?
I tried the following, but it doesn't seem correct.
db.Resources.find({Pools: {$elemMatch: { "$in", ['Pool1']}}}).pretty()
There are different ways to get what you want.
Find all records whose Pools' array contains Pool1:
db.Resources.find({Pools: 'Pool1'}).pretty()
Find all records whose Pools' array contains the following array elements, the order does not matter
db.Resources.find({Pools: {$all: ['Pool1', ...]}}).pretty()
To read more on querying arrays, see this mongodb post
This question already has answers here:
Multiple Counts with single query in mongodb
(3 answers)
Closed 4 years ago.
I need to do multiple count queries on the same collection with 3 different conditions at the same time.
Date <= x
Date <= y
Date <= z
Right now, I do:
collection.count(query1, function() {
collection.count(query2, function() {
collection.count(query3, function() {
Is there a way I can do all 3 queries in a single mongodb query.
Use facet in mongo 3.4 version:
db.col.aggregate(
{"$facet":{
"count1":[{"$match":query1},{"$count":"count"}],
"count2":[{"$match":query2},{"$count":"count"}],
"count3":[{"$match":query3},{"$count":"count"}]
}})
This question already has an answer here:
MongoDB sort with a custom expression or function
(1 answer)
Closed 5 years ago.
Let's say I have a collection with documents that look like this:
{
_id: <someMongoId>,
status: 'start'
otherImportantData: 'Important!'
}
...and status can be 'start', 'middle', or 'end'.
I want to sort these documents (specifically in the aggregation framework) by the status field - but I don't want it in alphabetical order; I want to sort in the order start -> middle -> end.
I've been looking for some way to project the status field to a statusValue field that is numeric (and where I get to dictate the numbers each string maps to), although I'd be happy to look at any alternatives.
Let's say I could do that. I'd take status and map it as such:
start: 1
middle: 2
end: 3
<anything else>: 0
then I could do something like this in the aggregation pipeline:
{
$sort: { statusValue : 1 }
}
...but I'm not sure how to get those statuses mapped in the aggregation pipeline.
If there is no way to do it, at least I'll know to stop looking. What would be the next best way? Using the Map-Reduce features in MongoDB?
You can try below aggregation in 3.4.
Use $indexOfArray to locate the position of search string in list of values and $addFields to keep the output index in the extra field in the document followed by $sort to sort the documents
[
{"$addFields":{ "statusValue":{"$indexOfArray":[[start, middle, end], "$status"]}}},
{"$sort":{"statusValue":1}}
]