Использовать выражение mon goose из внутреннего массива - PullRequest
0 голосов
/ 05 августа 2020

у меня модели выглядят так, openPositionsDetail - это массив объектов

const accountSummary = new Schema({
    account: {
        account: SchemaType.String,
        pialangCode: SchemaType.String,
        BranchOffice: SchemaType.String,
    },
    openPositionsDetail: [],
    qry_obj: {},
    idStatement: Number,
    createdBy: String

});

Я хочу получить данные этого документа, если хотя бы в openPositionsDetail. net не 0 (не 0, включая отрицательное число или положительное number) это мой документ выглядит как

{
    "_id": ObjectId("5f2a35baec826fa4a9eaa186"),
    "openPositionsDetail": [
        {
            "buy": NumberInt("332"),
            "sell": NumberInt("310"),
            "locking": NumberInt("350"),
            "net": NumberInt("300"),
            "product": NumberInt("5"),
            "productName": "XUL10"
        },
        {
            "buy": NumberInt("5"),
            "sell": NumberInt("3"),
            "locking": NumberInt("350"),
            "net": NumberInt("0"),
            "product": NumberInt("5"),
            "productName": "HKK"
        }
    ],
    "account": {
        "account": "RZBJ2889",
        "pialangCode": "EWF",
        "BranchOffice": "SSC"
    },
    "idStatement": NumberInt("25009"),
    "createdBy": "RIZAL",
    "qry_obj": {
        "ntm": NumberInt("493500"),
        "dtm": NumberInt("283500")
    },
    "__v": NumberInt("0")
},
//2
{
    "_id": ObjectId("5f2a35a1ec826fa4a9ea41bb"),
    "openPositionsDetail": [
        {
            "buy": NumberInt("635"),
            "sell": NumberInt("1"),
            "locking": NumberInt("1"),
            "net": NumberInt("634"),
            "product": NumberInt("5"),
            "productName": "XUL10"
        }
    ],
    "account": {
        "account": "RDZ21797",
        "pialangCode": "RFB",
        "BranchOffice": "DBS"
    },
    "idStatement": NumberInt("486"),
    "createdBy": "RIZAL",
    "qry_obj": {
        "ntm": NumberInt("887810"),
        "dtm": NumberInt("444010")
    },
    "__v": NumberInt("0")
}

// 3
{
    "_id": ObjectId("5f2a35b6ec826fa4a9ea92ca"),
   "openPositionsDetail": [
        {
            "buy": NumberInt("1"),
            "sell": NumberInt("1"),
            "locking": NumberInt("5"),
            "net": NumberInt("0"),
            "product": NumberInt("5"),
            "productName": "XUL10"
        },
        {
            "buy": NumberInt("3"),
            "sell": NumberInt("3"),
            "locking": NumberInt("0"),
            "net": NumberInt("0"),
            "product": NumberInt("6"),
            "productName": "JPK"
        }
    ],
    "account": {
        "account": "RDW22175",
        "pialangCode": "RFB",
        "BranchOffice": "DBS"
    },
    "idStatement": NumberInt("21237"),
    "createdBy": "RIZAL",
    "qry_obj": {
        "ntm": NumberInt("572250"),
        "dtm": NumberInt("286650")
    },
    "__v": NumberInt("0")
}

это мой запрос прямо сейчас, но я не уверен, что это будет работать с данными массива

AccountSummary.aggregate([
          { $sort: { 'account.account':  1} },
          {
            $match: {
              $and: filter.concat({
                $expr: { $not: {"openPositionsDetail.net": 0} }  
              })
            },
          },
          {
            $facet: {
              "stage1": [{ "$group": { _id: null, count: { $sum: 1 } } }],
              "stage2": [{ "$skip": data.offset }, { "$limit": data.limit }]
            }
          },
          { $unwind: "$stage1" },
          {
            $project: {
              count: "$stage1.count",
              data: "$stage2"
            }
          }
        ])

im новичок в mongodb

1 Ответ

2 голосов
/ 05 августа 2020

Вы можете сделать, как показано ниже

  1. Расслабьтесь

  2. Тогда проверьте не 0 условие

  3. Группа обратно.

Обновление:

Рабочий образец -

играть

db.collection.aggregate([
  {
    "$unwind": "$openPositionsDetail"
  },
  {
    "$project": {
      "notZero": {
        "$ne": [
          "$openPositionsDetail.net",
          0
        ]
      },
      "openPositionsDetail": 1
    }
  },
  {
    "$match": {
      notZero: true
    }
  },
  {
    "$group": {
      "_id": "$_id",
      data: {
        $push: "$$ROOT"
      }
    }
  }
])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...