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?
Related
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:
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
I have a MongoDB 3.6 database and I have several documents in one collection.
All documents have the same properties like:
{"type": "stuff", "price1": 2, "price2": 5, "total_cost": }
I would like to have the sum of price1 and price2 as value for total_cost, is it possible to do that?
And if I update price1 or price2, it will update automatically total_cost.
{"type": "stuff", "price1": 2, "price2": 5, "total_cost": 7 }
Thanks a lot!
You can search about the $out stage of the aggregation pipeline, but I think it will create a new collection in your database. To do your job, you need to compute your total in your code and $set after the result in your document.
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}}
]
In a collection of 130k elements with the structure:
{
"tags": ["restaurant", "john doe"]
}
There are 40k documents with "restaurant" tag but only 2 with "john doe". So the next queries are different:
// 0.100 seconds (40.000 objects scanned)
{"tags": {$all: [/^restaurant/, /^john doe/]}}
// 0.004 seconds (2 objects scanned)
{"tags": {$all: [/^john doe/, /^restaurant/]}}
It's there a way to optimize the query without sorting the tags in the client? The only way I can imagine now is putting less frequent tags at start of the search array.
I found a request feature for this in mongodb team JIRA:
https://jira.mongodb.org/browse/SERVER-1000
I implemented a stadistic system to put tags with more cadinality at the end of the array.