shanmugharaj
shanmugharaj

Reputation: 3924

How to add a new field in aggregation in projection with blank value in mongodb

This is my code.

db.article.aggregate(
{ $project : {
    title : 1 ,
    stats : {
        pv : "$name",
        foo : "$foo",
        dpv : 1
    }
}}
);

In that name,foo are there in collection. That dpv is not, but i need to add that one.

OUTPUT CAME

{
PV:"shan",
foo:"sd"
}

I need that dpv also in the ouput with null value.

Is it possible ? How to achieve this ? I am new to mongoDb. (using node.js)

Upvotes: 0

Views: 1144

Answers (2)

xthule
xthule

Reputation: 59

Asya is correct in that you need to specify a literal 1, and while the solution works a more correct solution would be:

dpv: {$literal: 1}

Upvotes: 3

Asya Kamsky
Asya Kamsky

Reputation: 42352

The syntax fieldname:1 means "pass through this field as is.

You want to have a literal 1 value - the simplest way is to create an expression that will return 1. I suggest: dpv:{$add:[1]}

Upvotes: 0

Related Questions