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"
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:
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 answers here:
Get distinct records values
(6 answers)
Closed 6 years ago.
I am a complete beginner in mongodb. I want to do the mongodb equivalence of sql select distinct column. i.e. For a mongodb collection with the following schema:
{
"_id" : ObjectId("xxxxxxcbf"),
"date" : "1462514997209",
"user_ids" : [
"userXXXX"
],
"created" : 1462543716,
"processed" : 1462543716
}
How to find unique user_ids in the collection?
You need to use distinct() like (assuming your collection name is collection1)
db.collection1.distinct( "user_ids" )
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.