【发布时间】:2018-11-23 07:54:13
【问题描述】:
我创建了一个 mongo 数据库,其中包含一个名为“rooms”的集合。
我想得到最后一个房间来创建下一个房间的 id。
你可以直接看到我恶心的代码:
房间架构
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
var Schema = mongoose.Schema;
var RoomSchema = mongoose.Schema({
name: {
type: String,
index: true
},
users: [{
type: Schema.Types.ObjectId,
ref: 'user'
}],
messages: {
type: [String]
},
subject: {
type: String
},
id: {
type: Number
}
});
var Room = module.exports = mongoose.model('Room', RoomSchema);
module.exports.createRoom = function (newRoom, callback) {
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(newRoom.password, salt, function (err, hash) {
newRoom.password = hash;
newRoom.save(callback);
});
});
}
试图得到最后一个房间
var lastRoom = Room.find({}).sort({$natural:-1}).limit(1);
然后我尝试运行它并输出“lastRoom”,我得到了这个:
the last room was: [object Object]
我该怎么办?
【问题讨论】:
-
我在后端做
标签: database mongodb collections record