组规范必须包含_id

时间:2019-03-03 20:55:18

标签: mongodb mongoose aggregate

我无法对组数进行简单匹配。

他强迫我设置_id,但是我已经在应用火柴了

  

(节点:11004)DeprecationWarning:不推荐使用collection.ensureIndex。   请改用createIndexes。 O erro -----> {MongoError:一组   规范必须包含_id       在queryCallback(C:\ stattrak \ stattrak-rest \ node_modules \ mongodb-core \ lib \ cursor.js:248:25)       在C:\ stattrak \ stattrak-rest \ node_modules \ mongodb-core \ lib \ connection \ pool.js:532:18       在process.internalTickCallback(internal / process / next_tick.js:70:11)的操作时间:时间戳{    bsontype:“时间戳”,低:4,高_:1551646370},确定:0,错误码:“组规范必须包含_id”,代码:15955,
  codeName:“ Location15955”,“ $ clusterTime”:{clusterTime:         时间戳{ bsontype:'Timestamp',low :4,high_:1551646370},        签名:{哈希:[二进制],keyId:[长]}},名称:“ MongoError”,[Symbol(mongoErrorContextSymbol)]:{}}

router.get('/update', async (req, res) => {
  try {
    const result = await Historie.aggregate(
      [
        {
          $match: {
            user: req.userId
          },
        },
        {
          $group: {
              _id: {
                user: req.userId
              },
              totalAmount: { $sum: "$points" },
              count: { $sum: 1 }
          }
        }
      ]
   )
   console.log('--------> User: ', req.userId)
   console.log('-------------->', result)
   const user = await User.findByIdAndUpdate(req.userId, { $set: { points: result[0].totalAmount } })
    return res.send({ user });
  } catch (error) {
    console.log('O erro -----> ', error)
    return res.status("400").send({ error: "Error at update user points" });
  }
})

历史模型:

const HistorieSechema = new mongoose.Schema({
  name: {
    type: String,
    require: true
  },
  description: {
    type: String,
    require: true
  },
  type: String,
  points: Number,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  validAt: Date,
  createdAt: {
    type: Date,
    default: Date.now
  }
});

0 个答案:

没有答案