1
db.setting.aggregate([
  {
    $match: {
      status: true,
      deleted_at: 0,
      _id: {
        $in: [
          ObjectId("5c4ee7eea4affa32face874b"),
          ObjectId("5ebf891245aa27c290672325")
        ]
      }
    }
  },
  {
    $lookup: {
      from: "site",
      localField: "_id",
      foreignField: "admin_id",
      as: "data"
    }
  },
  {
    $project: {
      name: 1,
      status: 1,
      price: 1,
      currency: 1,
      numberOfRecord: {
        $size: "$data"
      }
    }
  },
  {
    $sort: {
      numberOfRecord: 1
    }
  }
])

how to push the currency into price object using project please guide thanks a lot, also eager to know what is difference between $addtoSet and $push, what is good option to opt it from project or fix it from $addField

https://mongoplayground.net/p/RiWnnRtksb4

Output should be like this:

[
  {
    "_id": ObjectId("5ebf891245aa27c290672325"),
    "currency": "USD",
    "name": "Menz",
    "numberOfRecord": 0,
    "price": {
      "numberDecimal": "20",
      "currency": "USD",
    },
    "status": true
  },
  {
    "_id": ObjectId("5c4ee7eea4affa32face874b"),
    "currency": "USD",
    "name": "Dave",
    "numberOfRecord": 2,
    "price": {
      "numberDecimal": "10",
     "currency": "USD"
    },
    "status": true
  }
]
Sunil Dubey
  • 123
  • 1
  • 10

2 Answers2

3

You can insert a field into an object with project directly, like this (field price):

$project: {
      name: 1,
      status: 1,
      price: {
        numberDecimal: "$price.numberDecimal",
        currency: "$currency"
      },
      numberOfRecord: {
        $size: "$data"
      }
    }

By doing it with project, there is no need to use $addField.

For the difference between $addToSet and $push, read this great answer.

XavierBrt
  • 1,179
  • 8
  • 13
  • Thanks for this https://mongoplayground.net/p/vwELMGuX4CR see this i have "$numberDecimal" amount in that case price: { numberDecimal: "$price.$numberDecimal", currency: "$currency" }, FieldPath field names may not start with '$' getting errror – Sunil Dubey Jun 02 '20 at 12:50
  • Yes, you have to remove the $ before numberDecimal, so it is : `"$price.numberDecimal"`. Also, remove the $ before numberDecimal in the database. See: https://mongoplayground.net/p/kCpPLxXRBcd – XavierBrt Jun 02 '20 at 12:56
  • Yes agree don't know why database adding $ while creating any idea if you have I Know The dollar ($) prefixed field '$numberDecimal' in 'price.$numberDecimal' is not valid for storage. – Sunil Dubey Jun 02 '20 at 12:59
  • Or is this possible to use project using '$numberDecimal' – Sunil Dubey Jun 02 '20 at 13:01
1

You can just set the object structure while projecting, so in this case there's no need for either $push or $addToSet.

{
    $project: {
      name: "1",
      status: 1,
      price: {
        currency: "$currency",
        numberDecimal: "$price.numberDecimal"
      },
      currency: 1,
      numberOfRecord: {
        $size: "$data",

      }
    }
  }

Now the difference between $push and $addToSet is pretty trivial and derived from the name, $push saves all items while $addToSet will just create a set of them, for example:

input:

[
    //doc1
    {
        item: 1
    },
    //doc2
    {
        item: 2
    },
    //doc3
    {
        item: 1
    }
]

Now this:

{
    $group: {
        _id: null,
        items: {$push: "$item"}
    }
}

Will result in: {_id: null, items: [1, 2, 1]}

While:

{
    $group: {
        _id: null,
        items: {$addToSet: "$item"}
    }
}

Will result in: {_id: null, items: [1, 2]}

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43