does the addfields aggregate field actually add fields to the collection? - mongodb

I'm new to mongodb and aggregation. Specifically have a question about https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/
I am trying to create a pipeline where I have a need to create temporary variables to carry values from one stage to the next. I believe the way you do this is to use the 'addFields' operator. Does the addFields operator add these temporary fields to the collection? If so, is there a way around it, as I don't want to store any derivative fields into the database. I have no use for the temporary inter-stage fields after the aggregation operation is complete.

As the $addfields documentation points out, The added fields only apply to the document in the context of the pipeline.
That means the original document is not modified
You can add $addfields at any point in the pipeline, deriving fields from the data in previous stages

Related

MongoDB Bulk Find and Replace of ObjectId on a single Document

We have two documents that have merged and they now have one one ObjectId.
There exists a configuration document that may have references to the old ObjectId. The old ObjectID can exist all over this document which is full of nested arrays and lists.
We want to do a simple find and replace on this document, preferably without replacing the entire document itself.
Is there a generic way to set every field that has ObjectIdA as a value and replace it with ObjectIdB?
There's no way to do that, no. You need to perform updates on all possible paths explicitly.

MongoDB unwind empty array and not ignoring input document

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

MongoDB db.collection.save overwrite object when existing

In MongoDB, you can use db.collection.save({_id:'abc'}, objectToSave) to perform an upsert.
Let's define objectToSave as below
{_id:'abc', field1:1, field2:2};
In my collection, I have already have a document with same _id value as below:
{_id:'abc', field3:3};
The save function above will replace the existing document in collection to
{_id:'abc', field1:1, field2:2};
What I want is to perform a $set operation to produce some document in collection as below
{_id:'abc', field1:1, field2:2, field3:3};
Can this be achieved in the save function or I have to write separate update statements?
Note that objectToSave's fields are dynamic. The language I'm using is Node.JS.
db.collection.update({'_id':'abc'},{$set:{field1:1,field2:2}},{upsert:true})
should do what you want:
It upserts, so if the document does not exist yet, it is created as {_id:'abc',field1:1,field2:2} and efficiently so, since an index is used which must exist
If the document already exists, the fields field1 and field2 are set to the value in the update statement.
If either of the fields exist in the document, it will be overwritten.
Since you didn't state what language you use: in plain mongoDB, there is no save function. The explicit requirement to merge new and persisted versions of entities is quite an unusual one, so yes, I'd assume that you have to write a custom function.

mongodb computed field based on another query

I have a mongodb query, and I want to add a computed field. The computed field is based on where or not the item is in the results of another query. So my query returns the columns a,b,c,d, and then column e should be based on whether or not the current row would be matched by another query.
Is there an efficient way to do this in mongo? I'm not really sure how to do this one...
There is no way currently to execute a function as you describe within the database when returning a document via standard functions such as find. It's been requested by the community, but the general request is to operate only on a single document.
There are calculated fields using $project in the aggregation framework. But, they only operate on the current document in the pipeline. So, they can't summarize other queries.
You'll need to likely build your e value as part of your data access layer.

In Mongodb, how to retrieve the subset of an object that matches a condition?

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