Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add only a field from another collection in MongoDB

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.

like image 246
Prajval M Avatar asked Jun 24 '18 14:06

Prajval M


People also ask

How do I add a field to a collection in MongoDB?

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 .

How do you get data from two collections in MongoDB?

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.

How do I create a collection from another collection in MongoDB?

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.

How do I get only certain fields in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How to add new field to every document in MongoDB?

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);

What is $addfields in MongoDB?

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?

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.

How to add a new field to all collection in Salesforce?

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.


1 Answers

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

like image 70
Prajval M Avatar answered Oct 11 '22 01:10

Prajval M