In my MongoDB aggregation I am trying to determine if I can $unwind an array within an array if I specifically target an element of the initial array.
This is the data structure:
subscription: [
{
agency: []
}
]
What I want to do is unwind agency here, and since I know it will always be found within the first element of the subscription array, I'm doing this:
$unwind : {
path: "$subscription.0.agency"
}
However, this doesn't work. When I plug this in as the last stage in my aggregation (which works up until this step), I end up with zero docs returned. I'm not clear why. Seems like you should be able to do this. Can you simply not $unwind an array within an array, even if you denote a specific array element in the path?
I have a nested arrays as below in one of my collections,
how to retrieve the value from specific sub array & i don't need any of the other fields in that document.
I have tried using following query but it didn't get me the expected output,
query:
db.getCollection('my_collection').find({"_id":"08d4608a-e6c4-1bd8-80e6-8d1ac448c34b"},{"_id":0,"customProperties.0.1":1})
You can try
db.getCollection.find({
"someArray.someNestedArray.name": "value"
})
In this case How do I keep documents in aggregation with $unwind aggregation can be ommited but what if you have a more complex case where you need aggregation framework? How can you keep these documents and still unwind those who have non-empty lists?
My case involves aggregation pipeline with multiple stages (match, unwind, project, match, group,..), i simply cannot fallback to plain "match" because those nested arrays need to be presented in a form of report (JasperReports). I first thought that this $unwind empty array will solve my problem but it won't because this array needs to be empty since I'm later grouping these values in JasperReport and showing them as they are.
I hope I was clear enough.
Any ideas?
Starting from MongoDb 3.2, $unwind operator supports preserveNullAndEmptyArrays:<boolean>.
So when preserveNullAndEmptyArrays:true, it will also include value which doesn't have any data or empty data.
for more info, visit - https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/#document-operand-with-options
What I'm trying to do:
Filter a field of a collection that matches a given condition. Instead of returning every item in the field (which is an array of items), I only want to see matched items.
Similar to
select items from test where items.histPrices=[10,12]
It is also similar to what's found on the mongodb website here: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
Here's what I have been trying:
db.test.save({"name":"record", "items":[{"histPrices":[10,12],"name":"stuff"}]})
db.test.save({"name":"record", "items":[{"histPrices":[10,12],"name":"stuff"},
{"histPrices":[12,13],"name":"stuff"},{"histPrices":[11,14],"name":"stuff"}]})
db.test.find({},{"name":1,"items.histPrices":[10, 12]})
It will return all the objects that have a match for items.histPrices:[10,12], including ALL of the items in items[]. But I don't want the ones that don't match the condition.
From the comments left on Mongodb two years ago, the solution to get only the items with that histPrices[10,12] is to do it with javascript code, namely, loop through the result set and filter out the other items.
I wonder if there's a way to do that with just the query.
Your find query is wrong
db.test.find({},{"name":1,"items.histPrices":[10, 12]})
Your condition statement should be in the first part of the find statement.In your query {} means fetch all documents similar to this sql
select items from test (no where clause)
you have to change your mongodb find to
db.test.find({"items.histPrices":[10, 12]},{"name":1})
make it work
since your items is an array and if you wanted to return only the matching sub item, you have to use positional operator
db.test.find({"items.histPrices":[10, 12]},{"name":1,'items.$':1})
When working with arrays Embedded to the Document, the best approach is the one suggested by Chien-Wei Huang.
I would just add another aggregation, with the $group (in cases the document is very long, you may not want to retrieve all its content, only the array elements) Operator.
Now the command would look like:
db.test.aggregate({$match:{name:"record"}},
{$unwind:"$items"},
{$match {"items.histPrices":[10, 12]}},
{$group: {_id: "$_id",items: {$push: "$items"}}});)
If you are interested to return only one element from the array in each collection, then you should use projection instead
The same kind of issue solved here:
MongoDB Retrieve a subset of an array in a collection by specifying two fields which should match
db.test.aggregate({$unwind:"$items"}, {$match:{"items.histPrices":[10, 12]}})
But I don't know whether the performance would be OK. You have to verify it with your data.
The usage of $unwind
If you want add some filter condition like name="record", just add another $march at first, ex:
db.test.aggregate({$match:{name:"record"}}, {$unwind:"$items"}, {$match:{"items.histPrices":[10, 12]}})
https://jira.mongodb.org/browse/SERVER-828
Get particular element from mongoDB array
MongoDB query to retrieve one array value by a value in the array
I am using MongoDB with node.js. Is it possible to persist data as an (unordered) set in MongoDB directly? Or do I have to either
persist them as an array and transform it into a set in JavaScript
or
persist them as an object in the form of {entry1: true, entry2: true, ...}
If I have to do it in either of the two ways above, which one is more efficient? Considering I need to add/remove items frequently, but rarely need to re-assign the whole set/array.
You can manipulate an array as a set in MongoDB... relevant operators include:
$addToSet - The $addToSet operator adds a value to an array only if the value is not in the array already.
$push - The $push operator appends a specified value to an array.
$pop - The $pop operator removes the first or last element of an array.
$pull - The $pull operator removes all instances of a value from an existing array.
$elemMatch - The $elemMatch operator matches more than one component within an array element.