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"}]
}})
Related
This question already has answers here:
Firestore compound query with <= & >=
(1 answer)
Firestore compound query - combining not-in and inequality?
(2 answers)
Firestore | Why do all where filters have to be on the same field?
(2 answers)
Closed 5 months ago.
I'm trying to filter my posts such that I only get the ones that have time that is in the future and that don't include the users posts. So my code is:
func fetchAllPosts() async throws -> [Post] {
try await fetchPosts(from: postsReference.whereField("time", isGreaterThan: Date()).whereField("author.id", isNotEqualTo: user.id))
}
However when I run the code I get this error:
Thread 2: "Invalid Query. All where filters with an inequality (notEqual, lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same field. But you have inequality filters on 'time' and 'author.id'".
So does this mean that there is no way to filter my posts using inequality filters that work on two or more fields?
Thanks!
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"
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}}
]
This question already has answers here:
Referring to a select aggregate column alias in the having clause in Postgres
(2 answers)
Closed 6 years ago.
I'm creating a literal inside my select query, and at the end I'm trying to use having to find results that have a distance < 50000.
Coming from a MySql background, I'd think that this query should work.
SELECT "description",
"location",
ST_Distance_Sphere(location, ST_MakePoint(-126.4,45.32)) AS "distance"
FROM "news_agencies" AS "news_agencies"
HAVING distance < 50000;
I also tried adding quotes around the having >>>"distance"<<< text. If I remove 'having distance < 50000', my query runs fine and calculates the distance. Here is my error:
column "distance" does not exist
You cannot use alias name in a having clause. Try like this:
select * from
(
SELECT "description", "location",
ST_Distance_Sphere(location, ST_MakePoint(-126.4,45.32)) AS "distance"
FROM "news_agencies" AS "news_agencies") t
where distance < 50000;
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.