2015-06-28 64 views
1

当使用findOneAndUpdate将upsert和setDefaultsOnInsert设置为true时,我尝试在数据库中插入新用户时遇到问题。我所试图做的是设置以下模式的默认值:Mongoose setDefaultsOnInsert和2dsphere索引不起作用

var userSchema = new mongoose.Schema({ 
    activated: {type: Boolean, required: true, default: false}, 
    facebookId: {type: Number, required: true}, 
    creationDate: {type: Date, required: true, default: Date.now}, 
    location: { 
     type: {type: String}, 
     coordinates: [] 
    }, 
    email: {type: String, required: true} 
}); 

userSchema.index({location: '2dsphere'}); 

findOneAndUpdate代码:

model.user.user.findOneAndUpdate(
     {facebookId: request.params.facebookId}, 
     { 
      $setOnInsert: { 
       facebookId: request.params.facebookId, 
       email: request.payload.email, 
       location: { 
        type: 'Point', 
        coordinates: request.payload.location.coordinates 
       } 
      } 
     }, 
     {upsert: true, new: true, setDefaultsOnInsert: true}, function (err, user) { 
      if (err) { 
       console.log(err); 
       return reply(boom.badRequest(authError)); 
      } 
      return reply(user); 
     }); 

正如你可以看到我还可以存储用户的纬度和经度,这就是问题开始的地方。当findOneAndUpdate被称为我得到这个错误:

{ [MongoError: exception: Cannot update 'location' and 'location.coordinates' at the same time] 
name: 'MongoError', 
message: 'exception: Cannot update \'location\' and \'location.coordinates\' at the same time', 
errmsg: 'exception: Cannot update \'location\' and \'location.coordinates\' at the same time', 
code: 16836, 
ok: 0 } 

当我删除了2dsphere指数和所有与位置相关的代码,它不会把我的creationDate。我做错了什么?

回答

2

setDefaultsOnInsert选项使用$setOnInsert操作执行其功能,而且看起来这是相互矛盾的用自己的使用$setOnInsert设置location

一种解决方法是删除setDefaultsOnInsert选项,并把它们都放在自己的$setOnInsert运营商:

model.user.user.findOneAndUpdate(
     {facebookId: request.params.facebookId}, 
     { 
      $setOnInsert: { 
       activated: false, 
       creationDate: Date.now(), 
       email: request.payload.email, 
       location: { 
        type: 'Point', 
        coordinates: request.payload.location.coordinates 
       } 
      } 
     }, 
     {upsert: true, new: true}, 
     function (err, user) { 
      if (err) { 
       console.log(err); 
       return reply(boom.badRequest(authError)); 
      } 
      return reply(user); 
     }); 
+0

我正在考虑这样做,但这是否意味着猫鼬中存在一个错误? – Jdruwe

+1

@Jdruwe是的,我认为这个问题是Mongoose中的一个错误。在这种情况下,它不应该试图将'location.coordinates'设置为空数组。 – JohnnyHK

+1

好的,我在github回购上创建了一个问题,谢谢你的时间! – Jdruwe

0

其中一个开发者的回答我的问题,在GitHub上,并添加它作为4.0的一个里程碑。 8日发布,这是他的解决方法:

$setOnInsert: { 
      facebookId: request.params.facebookId, 
      email: request.payload.email, 
      'location.type': 'Point', 
      'location.coordinates': request.payload.location.coordinates 
    } 

他的解决方案并没有为我工作,我得到以下错误:

{ [MongoError: exception: insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('559024a0395dd599468e4f41'), facebookId: 1.020272578180189e+16, location: { coordinates: [], type: "Point" }, email: "[email protected]", gender: "male", lastName: "Druwé", firstName: "Jeroen", __v: 0, activated: false, creationDate: new Date(1435509921264), familyDetails: { children: [] } }] 
+0

当我试图摆脱错误,但'location.coordinates'结束为'[]'而不是来自请求的坐标。 – JohnnyHK

+0

我也收到了一个空的[]和我在答案中添加的错误。奇怪的是你没有得到这个MongoError。 – Jdruwe