I'm dealing with a specific and simple case. I need some help, because I don't seem to be able to find the solution for this.
I've created a simple Schema and Model for MongoDB inside a javascript class.
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);
This is in the constructor and I already have a lot of database interactions working, but I can't manage to get this one working.
I call this method for adding the new document:
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.
This works fine. I can see the document is created correctly ,with Mongo Explorer, and if I use a .find() to retrieve everything from the collection, it shows up.
Then, I try to look for the inserted Doc with:
FindCalendarViewUser : function(Id){
this.CalendarViewUser.findOne(
{ OwnerID : Id },
function(err,result){
if(err) {console.log(err);}
else{console.log(result);}
}
);
}
Here, it returns result = null.
The "Id" parameter is a String in both methods, just like the "OwnerId" in the Schema.
What am I doing wrong?
Both "OwnerId" and "Id" are the same. I also tried converting both to ObjectId, with no success.
Sorry for the code formatting.
EDIT:
Basically, I can find the Document using the _id field:
{ _id : '4fb0d1702edfda0104fc2b9f'}
But I can't find it with the OwnerId field:
{ OwnerId : '4f55e1a6c2c7287814000001'}
These values were taken directly from the database, so they correspond to the actual stored values.
EDIT 2:
I just figured out that if I insert the value by hand:
$set: { OwnerId : '4f55e1a6c2c7287814000001' }
and then search by the exact same string, it returns the document!
this.CalendarViewUser.findOne(
{ OwnerID : '4f55e1a6c2c7287814000001'}
...
So I guess it is inserting some kind of garbage in the document's OwnerID field, or there was some kind of incompatibility with the field's Type. I will post the results.
typo?
if(err) {console.log(err);}
else{console.log(err);}
}
It turns out, the callback function of the FindCalendarViewUser function needed parenteses around itself... it works now, just by adding those parentesis...
FindCalendarViewUser : function(Id){
this.CalendarViewUser.findOne(
{ OwnerID : Id },
(function(err,result){
if(err) {console.log(err);}
else{console.log(result);}
})
);
}
Here's the working example. Jesus christ why. I could have sworn I had used without that in other ocasions.
EDIT: And now it is working without the parenteses. I'm sorry. It must be some tiny issue I have around the code. I just can't find it.