var n = new Chat();
n.name = "chat room";
n.save(function(){
//console.log(THE OBJECT ID that I just saved);
});
Run Code Online (Sandbox Code Playgroud)
我想在console.log中保存我刚刚保存的对象的对象id.我如何在Mongoose中做到这一点?
我在Google上搜索了几天,我尝试了很多东西,但我仍然无法在我的用户集上进行良好的全文搜索.
我试过ElasticSearch但是查询和分页几乎是不可能的......
我为Mongoose尝试了许多插件,如ElMongo,mongoose-full-text,Mongoosastic等等...每个人都记录不好,我不知道如何进行良好的全文搜索.
所以,我的收藏是一个普通的收藏:
user = {
name: String,
email: String,
profile: {
something: String,
somethingElse: String
}
}
Run Code Online (Sandbox Code Playgroud)
我在页面中有一个简单的搜索输入POST,如果我输入hello world我需要的是在整个集合字段中搜索我的搜索查询的匹配单词并获得结果.
如果有选项可以处理每页10个项目的分页,那将是非常好的...
实现这一目标的最佳解决方案是什么?我正在使用MongoDB 2.6.*与Mongoose,NodeJS和ExpressJS.
谢谢.
目前我使用save来添加单个文档.假设我有一个文档数组,我希望将其存储为单个对象.有没有办法通过单个函数调用添加它们,然后在完成后获得单个回调?我可以单独添加所有文档,但是管理回调以便在完成所有操作时解决问题.
我对Mongoose和MongoDB很新,所以我很难搞清楚这样的事情是否可行:
Item = new Schema({
id: Schema.ObjectId,
dateCreated: { type: Date, default: Date.now },
title: { type: String, default: 'No Title' },
description: { type: String, default: 'No Description' },
tags: [ { type: Schema.ObjectId, ref: 'ItemTag' }]
});
ItemTag = new Schema({
id: Schema.ObjectId,
tagId: { type: Schema.ObjectId, ref: 'Tag' },
tagName: { type: String }
});
var query = Models.Item.find({});
query
.desc('dateCreated')
.populate('tags')
.where('tags.tagName').in(['funny', 'politics'])
.run(function(err, docs){
// docs is always empty
});
Run Code Online (Sandbox Code Playgroud)
这有更好的方法吗?
编辑
为任何困惑道歉.我要做的是获取包含有趣标签或政治标签的所有项目.
编辑
没有where子句的文件: …
如果mongoose无法连接到我的数据库,如何设置错误处理的回调?
我知道
connection.on('open', function () { ... });
Run Code Online (Sandbox Code Playgroud)
但是有类似的东西
connection.on('error', function (err) { ... });
Run Code Online (Sandbox Code Playgroud)
?
我有一个问题,我希望能够获得集合的所有独特城市,我的代码看起来像这样:
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var PersonSchema = new Schema({
name: String,
born_in_city: String
});
var Person = mongoose.model('Person', PersonSchema);
Run Code Online (Sandbox Code Playgroud)
在原生的MongoDb中,我可以做到db.person.distinct("born_in_city"),但似乎没有任何等同于Mongoose.是我自己迭代所有文档的唯一选择,还是有更好的解决方案?
为了尝试使用node-mongodb-native回复者建议的底层,我尝试这样做:
mongoose.connection.db.collections(function(err, collections){
collections[0].distinct('born_in_city', function( err, results ){
console.log( err, results );
});
});
Run Code Online (Sandbox Code Playgroud)
然而,它results是空的,没有错误.我也希望能够只按名称获取所需的集合,而不是必须过滤collections返回的内容.
我以这种方式将jongoose docs作为json返回:
UserModel.find({}, function (err, users) {
return res.end(JSON.stringify(users));
}
Run Code Online (Sandbox Code Playgroud)
但是,用户.__ proto__也被退回了.没有它我怎么能回来?我尝试了这个,但没有奏效:
UserModel.find({}, function (err, users) {
return res.end(users.toJSON()); // has no method 'toJSON'
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Mongoose,MongoDB和Node.
我想定义一个架构,其中一个字段是date\timestamp.
我想使用此字段以返回在过去5分钟内已更新的所有记录.
由于在Mongoose中我不能使用Timestamp()方法,我理解我唯一的选择是使用以下Javascript方法:
time : { type: Number, default: (new Date()).getTime() }
Run Code Online (Sandbox Code Playgroud)
它可能不是查询庞大数据库的最有效方式.如果有人可以分享一种更有效的方式来实现这一点,我将非常感激.
有没有办法用Mongoose实现这个并且能够使用MongoDB时间戳?
我正在用ExpressJS,PassportJS,MongoDB和MongooseJS编写NodeJS服务器.我设法让PassportJS使用通过Mongoose获得的用户数据进行身份验证.
但要使其工作,我必须使用如下所示的"findById"函数.
var UserModel = db.model('User',UserSchema);
UserModel.findById(id, function (err, user) { < SOME CODE > } );
Run Code Online (Sandbox Code Playgroud)
UserModel是一个猫鼬模型.我之前声明了架构UserSchema.所以我想UserModel.findById()是一种Mongoose模型的方法?
题
它findById做了什么,有什么文件吗?我用Google搜索了一下但没有发现任何东西.
我已经定义了一个mongoose用户架构:
var userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true},
password: { type: String, required: true},
name: {
first: { type: String, required: true, trim: true},
last: { type: String, required: true, trim: true}
},
phone: Number,
lists: [listSchema],
friends: [mongoose.Types.ObjectId],
accessToken: { type: String } // Used for Remember Me
});
var listSchema = new mongoose.Schema({
name: String,
description: String,
contents: [contentSchema],
created: {type: Date, default:Date.now}
});
var contentSchema = new mongoose.Schema({
name: String,
quantity: String, …Run Code Online (Sandbox Code Playgroud)