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
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:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Mongodb sort inner array
(3 answers)
How to sort a collection using the last element of an array
(2 answers)
Closed 3 years ago.
We're attempting to port over our real estate property search from SQL Server to MongoDB. Every property can have multiple listings, which we're storing directly in each record as a child array under the property like so:
{
"propertyId": 18023335652,
"latitude": 33.67654,
"longitude": -117.790335,
"listings": [{
"orgId": "",
"listingId": "",
"offMarketDate": "2001-07-06T00:00:00",
"soldPrice": 273000,
"bedrooms": 3,
"bathrooms": 3,
"livingAreaInSqFt": 1653,
"yearBuilt": 1980,
"rank": 3
},
{
"orgId": "caclaw-n",
"listingId": "11234029",
"offMarketDate": "2015-02-12T00:00:00",
"soldPrice": 325000,
"bedrooms": 4,
"bathrooms": 3,
"livingAreaInSqFt": 1646,
"yearBuilt": 1980,
"rank": 2
}
]
}
}
When importing the properties/listings into MongoDB, we have business logic that determines a "rank" for each listing so we know which one is "preferred" and should be displayed for a given property to a certain user. It's not as simple as just setting a "isPreferred" value on each listing or using the listing with a Rank = 1 because the user executing the property search might not have access to certain listings (they're in a different MLS). I want to write a MongoDB query that does the following:
Filters properties by their listing values (e.g. Listings.Bedrooms = 3)
Filters each remaining property's listings to only include the preferred one (with the lowest Listings.Rank).
Sort the remaining properties by some field (e.g. Listings.Bedroom asc) but make sure the sort is applied to the preferred listing.
How would I go about doing this in MongoDB?
This question already has answers here:
How do I get a list of just the ObjectId's using pymongo?
(5 answers)
Closed 5 years ago.
I have collection of documents (~1 billion of items) and I want to get it as array of field. And in the same time I do not want to postprocess result of Mongo query.
Example:
// Collection looks alike
[
{"_id": ObjectId("...", "id": "12313123", ....)},
{"_id": ObjectId("...", "id": "35675468456", ....)}
{"_id": ObjectId("...", "id": "23233463", ....)}
....
]
// Desired result
["12313123", "35675468456", "23233463"]
I.e I want to get only field id and make result flatten. But statement
db.collection.find({}, {"_id": 0, "id": 1}) returns list of objects.
Would single-purpose aggregation db.collection.distinct("id") work for you?
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.