I am doing a query on my database using Mongoose to retrieve all documents in a collection. Currently there is only one document in the collection. It returns the document and looks fine, but I cannot access some of the properties.
Code snippet:
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
}).catch((err)=>{console.log(err)});
Result:
[
{
_id: 5f6d4dc312c76000170c5c43,
name: 'Bob',
email: 'bob#mail.com',
zipCode: '12345',
pageVisited: 'p1m2549',
dateTime: 2020-09-25T01:54:11.152Z,
ipAddr: '111.111.111.111',
__v: 0
}
]
5f6d4dc312c76000170c5c43
Bob
bob#mail.com
undefined
undefined
undefined
undefined
What could be causing this bizarre behavior? It really doesn't make any sense that I can access some of the properties but not others.
That could be because these elements not be defined in the Schema
Define Schema as mentioned below
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: String,
email: String,
zipCode: String,
pageVisited: String,
dateTime: Date,
ipAddr: String,
__v: Number
});
var User = mongoose.model('users', UserSchema );
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
console.log(response[0].__v);
}).catch((err)=>{console.log(err)});
Related
I have two schemas (using mongoose) like below:
var PostSchema = new Schema({
name: String,
content: String,
likes: [{
type: String,
ref: 'User'
}],
...
})
var UserSchema = new Schema({
name: String,
pinnedPosts: [{
type: String,
ref: 'Post'
}],
...
})
Now I want to get all posts with two new populated fields: isLiked and isPinned depend on the auth state. If the user hasn't signed in (auth is null) then all these two fields are false. If user has signed in and had liked the post, isLiked is true... Can I do it when I query the post?
I'm guessing because you save resources by making 1 request instead of 2 to the database. Is this significant? Should I care to use populate if I'm populating only 1 field (the advantage is clearer when you populate more than 1)?
You don't save resources by using populate. Under the hood mongoose calls the database as many times as required. Consider the example:
module.exports = function(){
var UserSchema = new Schema({
email : {type : String, required: true},
password: {type: String, required: true}
});
return mongoose.model("User", UserSchema);
};
module.exports = function(){
var AccountSchema = new Schema({
number : {type : String, required: true},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
return mongoose.model("Account", AccountSchema);
};
mongoose.set('debug', true); //to see queries mongoose is using behind the scenes
Account.find({}, function(err, res){
console.log(res)
}).populate("user")
Apart from the results, you'll see something like this on console:
Mongoose: accounts.find({}, { fields: undefined })
Mongoose: users.find({ _id: { '$in': [ ObjectId("5807d6d6aa66d7633a5d7025"), ObjectId("5807d6d6aa66d7633a5d7026"), ObjectId("5807d709aa66d7633a5d7027") ] } }, { fields: undefined })
That's mongoose finding account documents and then user for each one of them.
It's saving you a lot of code and I don't see why you should not use it irrespective of the number of fields you're populating.
Here is the common way to define a collection structure with Mongoose :
var UserSchema = new Schema({
_id: Schema.Types.ObjectId,
username: String,
...
});
And Now I want _id field declared as Number type :
var UserSchema = new Schema({
_id: Number,
username: String,
...
});
The problem is, do I need to declare more infomation about _id ? Such as :
var UserSchema = new Schema({
_id: {type: Number, required: true, index: {unique: true}},
username: String,
...
});
I am not sure whether MongoDB would do it automatically.
if you know the answer, could you leave a comment below ? Thank you!
Well, after some practice, I realized that, MongoDB would set _id as PRIMARY KEY (NOT NULL + UNIQUE INDEX) automatically. So, just type:
_id: Number,
...
This is my schemes:
var authUserScheme = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
}, {_id: false});
var usersSchema = mongoose.Schema({
// OTHER THINGS
auth : [ authUserScheme ],
// other things
});
When i set an 'auth' path, mongodb deletes the entire document, but i want to delete only the auth row when expire date... It is possible?
Sorry for my english, i speak spanish.
You can't use a TTL index to delete a portion of a document on expiry.
However, it looks like your authUserScheme is really more of a session concept than an embedded document.
A better approach would be to use a reference from the authUserScheme to the related user, eg:
var authUserSchema = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
user: { type: Number, ref: 'User' }
});
var userSchema = mongoose.Schema({
name: String,
// Other fields
})
var AuthUser = mongoose.model('AuthUser', authUserSchema);
var User = mongoose.model('User', userSchema);
This is my schema:
var Review = new Schema({
user: {type: ObjectId, ref:'User'},
lastModified: {type: Date, default: Date.now }
});
var Subject = new Schema({
name: String,
review: [Review],
...
});
The query will return all the subjects with review from a user.
{'review.user': id}
Is it possible to sort the result based on review.lastModified?
Subject.find({'review.user': id}).select('name').sort('???').exec( function(err, subjects){
if (err) return res.json(error);
console.log('subject', subjects);
});
You cannot sort within a document using MongoDB. Sorting within the document must be done at the application level.