Mongodb mongo插入到顶部的数组中,无重复项

Mongodb mongo插入到顶部的数组中,无重复项,mongodb,Mongodb,我正在尝试使用附加项更新ID数组,但我需要以下内容: 1.如果它存在,那么显然它不是重复的 2.如果添加了一个新项,它将在数组顶部完成 Schedule.update( { date: "10/4/2018" }, { $addToSet: { games: <id> }}, { upsert: true }, function(err, update){ }) Schedule.update( {日期:“10/4/2018”}, {$addToSet:{games:

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

Schedule.update(
  { date: "10/4/2018" },
  { $addToSet: { games: <id> }},
  { upsert: true },
function(err, update){

})
Schedule.update(
{日期:“10/4/2018”},
{$addToSet:{games:}},
{upsert:true},
功能(错误、更新){
})
谢谢

Kris

您可能希望使用该修改器:

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){

})
Schedule.update(
{date:“10/4/2018”,games:{$nin:}},//排除已经包含该值的文档
{ 
$push:{
游戏:{
每件$:[],
$position:0//推到第一个位置
}
}
},
{upsert:true},
功能(错误、更新){
})