mongo插入数组顶部而不重复

时间:2018-10-04 16:09:30

标签: mongodb

我正在尝试使用其他项目更新ID数组,但我需要以下内容: 1.如果存在,那么显然不是重复的 2.如果添加了新项目,它将在数组顶部完成

id: {
  type: Sequelize.INTEGER,
  primaryKey: true,
  autoIncrement: true // this converts INT to SERIAL for Postgres
},

谢谢

克里斯

1 个答案:

答案 0 :(得分:1)

您要为此使用$position修饰符:

Schedule.update(
  { date: "10/4/2018", games: { $nin: <id> } }, // exclude documents that already contain the <id> value
  { 
    $push: {
      games: {
        $each: [ <id> ],
        $position: 0 // push to first position
      }
    }
  },
  { upsert: true },
function(err, update){

})