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!
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"
This question already has answers here:
How to update Mongodb fields with omitempty flag in Golang structure
(2 answers)
Closed 2 years ago.
I have a Go function that updates a MongoDB document with a new value and it works with any value but 0. And I don't understand why. It returns a ModifiedCount of 0.
So for example, I can update the field "price" from 75 to 20. But it won't update it from 75 to 0. The important parts look like this:
type Car struct {
ID primitive.ObjectID `bson:"_id,omitempty" `
Price float64 `json:"price" form:"price" bson:"price,omitempty"`
}
...
updateResult, err := c.UpdateOne(ctx, filter, bson.M{"$set": update}, options)
where:
filter:
{ObjectID("5f1aa6da68ac05d7863e9b41")}
and update:
{ObjectID("5f1aa6da68ac05d7863e9b41") 0}
I'm not quite sure, but I think it's due to the omitempty rule on your bson, since nil can't be a valid value for float, the 0 value omit your field. More explanation here. If that field can be set as null value, I recommend you use this approach.
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}}
]
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.