插入后找不到文件
我正在处理一个具体而简单的案例。 我需要一些帮助,因为我似乎无法find解决scheme。
我已经在一个javascript类中为MongoDB创build了一个简单的Schema和Model。
this.UserSchema = new this.Schema({ UserId: { type: String }, IsToggle: { type: Boolean} }); this.CalendarViewUserSchema = new this.Schema({ _id: { type: this.Schema.ObjectId }, OwnerId: { type: String }, UserList: [ this.UserSchema ] }); this.CalendarViewUser = this.mongoose.model('CalendarViewUsers',this.CalendarViewUserSchema);
这是在构造函数中,我已经有很多数据库交互工作,但我无法设法得到这个工作。
我将这种方法称为添加新文档:
AddCalendarViewUser : function(Id, UserToAddId){ NewUser = { UserId : UserToAddId, IsToggle : false }; this.CalendarViewUser.update( { OwnerId : Id }, { $set: { OwnerId : Id }, $push: { UserList : NewUser } }, {upsert:true}, function(err){ if(err) {console.log(err);} else {console.log('Success');} } ); }, // this comma just introduces the next method.
这工作正常。 我可以看到使用Mongo Explorer正确创build了文档,如果使用.find()从集合中检索所有内容,则会显示该文档。
然后,我尝试查找插入的文档:
FindCalendarViewUser : function(Id){ this.CalendarViewUser.findOne( { OwnerID : Id }, function(err,result){ if(err) {console.log(err);} else{console.log(result);} } ); }
在这里,它返回结果= null。
两个方法中的“Id”参数都是一个String,就像Schema中的“OwnerId”一样。
我究竟做错了什么?
“OwnerId”和“Id”是相同的。 我也尝试转换为ObjectId,没有成功。
对不起,代码格式。
编辑:
基本上,我可以使用_id字段find文档:
{ _id : '4fb0d1702edfda0104fc2b9f'}
但是我找不到与OwnerId字段:
{ OwnerId : '4f55e1a6c2c7287814000001'}
这些值直接来自数据库,所以它们对应于实际存储的值。
编辑2:
我只知道如果我手动插入值:
$set: { OwnerId : '4f55e1a6c2c7287814000001' }
然后用完全相同的stringsearch,它返回文档!
this.CalendarViewUser.findOne( { OwnerID : '4f55e1a6c2c7287814000001'} ...
所以我想它是在文档的OwnerID字段中插入某种垃圾,或者与字段的Type有某种不兼容。 我会公布结果。
错字?
if(err) {console.log(err);} else{console.log(err);} }
事实certificate,FindCalendarViewUser函数的callback函数需要在自身周围进行…现在可以工作,只需添加这些parentesis …
FindCalendarViewUser : function(Id){ this.CalendarViewUser.findOne( { OwnerID : Id }, (function(err,result){ if(err) {console.log(err);} else{console.log(result);} }) ); }
这是工作的例子。 耶稣基督为什么。 我可以发誓我没有在其他方面使用过。
编辑:现在它正在没有parenteses工作。 对不起。 这是代码中的一个小问题。 我无法find它。