Node.js 为什么我在mongodb中的字段现在为空?
每次我在mongo db中插入一个类似Node.js 为什么我在mongodb中的字段现在为空?,node.js,mongodb,Node.js,Mongodb,每次我在mongo db中插入一个类似{text:data.text,userId:'5454234ddsc32fcds23dfe3'}的字段。插入更多类似这样的字段后,其中一个字段保持正常,其余字段都为空。我该怎么办?我想mongo会像看到重复的一样看到它。我想我应该允许重复。这是我的模式 var ConversationsSchema=new Schema({ conversations:{type:Array, text:String, sender:String,
{text:data.text,userId:'5454234ddsc32fcds23dfe3'}
的字段。插入更多类似这样的字段后,其中一个字段保持正常,其余字段都为空。我该怎么办?我想mongo会像看到重复的一样看到它。我想我应该允许重复。这是我的模式
var ConversationsSchema=new Schema({
conversations:{type:Array,
text:String,
sender:String,
to:String,
userId:{type:String,select:true,unique:false}
}
})
更新查询:
Conversations.update(({_id:data.conversationID},{$addToSet:{conversations:{text:data.textMessage,sender:data.sender,to:data.to,userId:data.userId}}}),function(err){
//console.log('Cannot update your conversation')
console.log(err);
})
谢谢你的帮助 更新!
架构必须是:
var ConversationsSchema=new Schema({
conversations:{type:Array,
text:String,
sender:String,
to:String,
userId:{type:String,select:true,index:{unique:false}}
}})
您正在插入一个对象,但架构需要一个ArrayNo!我在使用$addToSet的地方做了一个更新。我有一个对象数组,我只是在数组中引入了一个对象。不仅仅是这个语句是正确的,但是如果这是一个“对象数组”,那么这个方法就完全错了。看起来您正试图将
ObjectId
引用扩展为“User”对象。但是如果你想要一个对象数组,那么你的模式符号与此不同如果我每次都更改userId它工作…我需要允许userId重复,但我不知道如何…我尝试了unique:false但不工作显示你的查询/更新