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
  }
]

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this 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
Yes, you have to remove the $ before numberDecimal, so it is : "$price.numberDecimal". Also, remove the $ before numberDecimal in the database. See: mongoplayground.net/p/kCpPLxXRBcd
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.
Or is this possible to use project using '$numberDecimal'
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]}

3 Comments

Hey Tom thanks for explanation so no duplicate value if we use $addToSet
is it possible if we have $numberDecimal into price object get data like : mongoplayground.net/p/xDvOx0LL52G
In this case just use price instead of price.numberDecimal in the $project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.