I have two collections
A with schema
{
a : Array,
b : ObjectID
}
and B with the following schema
{
x : 'string',
y : // some object schema
...
b : ObjectID
}
I want to use mongo aggregate to add a new field in collections B to include only a from collection A searching using b.
I want my value after aggregation to have the following schema :
{
x : 'string',
newField : a // array from collection A
y : // some object schema
...
b : ObjectID
}
only $lookup does not work because i do not want the whole object and also merge will remove _id and merge others which i do not want.
To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .
For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.
MongoDB – copyTo() Method In MongoDB, copyTo() method is used to copies all the documents from one collection(Source collection) to another collection(Target collection) using server-side JavaScript and if that other collection(Target collection) is not present then MongoDB creates a new collection with that name.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
MongoDB Database Big Data Analytics To add new field to every document in a MongoDB collection, you can use $set operator. The syntax is as follows: db.yourCollectionName.update ({}, { $set: {"yourFieldName": yourValue} }, false, true);
The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields. Starting in version 4.2, MongoDB adds a new aggregation pipeline stage $set that is an alias for $addFields. { $addFields: { < newField >: < expression >, ... } }
What is Aggregation in MongoDB? Aggregation — as the literal meaning suggests it involves combining various things, similarly in MongoDb aggregation is a technique to query data from multiple collections by grouping or joining them followed by performing a variety of operations ( explained later in this doc) and then returning computed results.
In case you want to add a new_field to all your collection, you have to use empty selector, and set multi flag to true (last param) to update all the documents db.your_collection.update ( {}, { $set: {"new_field": 1} }, false, true ) In the above example last 2 fields false, true specifies the upsert and multi flags.
I'm answering this myself after solving it.
This can be done as a pipeline of operations as follows :
{
$lookup: {
from: "A",
localField: "b",
foreignField: "b",
as: "someField"
}
},
{
$addFields: {
newField : "$someField.a"
}
},
{
$unwind: "$newField"
},
{
$project: {
someField: 0
}
}
More efficient answers will be accepted
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With