我正在使用 mongoose 和 mocha 进行 MongoDB 模式设计和 API 开发我收到此警告...这是什么意思,它将如何影响我以及修复方法是什么?
在实际警告文本下方:
(node:9872) DeprecationWarning:监听 Db 类的事件已被弃用,将在下一个主要版本中删除。
Campaign.find {client_id:req.param('client_id')}, (error, campaigns) ->
if error
response =
error: error.message
else
for campaign in campaigns
query =
campaign_id: campaign._id
console.log query
CampaignResponse.find query, (err, campaignResponsesCount) ->
console.log campaignResponsesCount
response = campaigns
res.json response
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这不会返回任何结果.但是,有CampaignResponse具体的项目campaign._id.我很确定这是类型和演员的问题,但我无法弄清楚该怎么做.
有帮助吗?
我有以下简单的shema:
var userSchema = new Schema({
name : String,
age: Number,
_creator: Schema.ObjectId
});
var User = mongoose.model('User',userSchema);
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建新文档并返回到客户端,但我想从一个中排除'creator'字段:
app.post('/example.json', function (req, res) {
var user = new User({name: 'John', age: 45, _creator: 'some ObjectId'});
user.save(function (err) {
if (err) throw err;
res.json(200, {user: user}); // how to exclude the _creator field?
});
});
Run Code Online (Sandbox Code Playgroud)
最后,我想发送没有_creator字段的新创建用户:
{
name: 'John',
age: 45
}
Run Code Online (Sandbox Code Playgroud)
是否可以在没有额外发现请求的情况下制作猫鼬?
PS:最好是通过它
我正在为item.comments列表添加评论.我需要在我在响应中输出之前获取comment.created_by用户数据.我该怎么做?
Item.findById(req.param('itemid'), function(err, item){
var comment = item.comments.create({
body: req.body.body
, created_by: logged_in_user
});
item.comments.push(comment);
item.save(function(err, item){
res.json({
status: 'success',
message: "You have commented on this item",
//how do i populate comment.created_by here???
comment: item.comments.id(comment._id)
});
}); //end item.save
}); //end item.find
Run Code Online (Sandbox Code Playgroud)
我需要在res.json输出中填充comment.created_by字段:
comment: item.comments.id(comment._id)
Run Code Online (Sandbox Code Playgroud)
comment.created_by是我的mongoose CommentSchema中的用户引用.它目前只给我一个用户ID,我需要填充所有用户数据,密码和盐字段除外.
以下是人们提出的架构:
var CommentSchema = new Schema({
body : { type: String, required: true }
, created_by : { type: Schema.ObjectId, ref: 'User', index: true }
, created_at : { type: Date }
, …Run Code Online (Sandbox Code Playgroud) 试图在Typescript中实现Mongoose模型.搜索谷歌只揭示了混合方法(结合JS和TS).在没有JS的情况下,如何以我天真的方式实现User类?
希望能够在没有行李的情况下使用IUserModel.
import {IUser} from './user.ts';
import {Document, Schema, Model} from 'mongoose';
// mixing in a couple of interfaces
interface IUserDocument extends IUser, Document {}
// mongoose, why oh why '[String]'
// TODO: investigate out why mongoose needs its own data types
let userSchema: Schema = new Schema({
userName : String,
password : String,
firstName : String,
lastName : String,
email : String,
activated : Boolean,
roles : [String]
});
// interface we want to code to?
export interface IUserModel …Run Code Online (Sandbox Code Playgroud) 我找到的用于渲染具有猫鼬结果的页面的所有内容都说是这样做的:
users.find({}, function(err, docs){
res.render('profile/profile', {
users: docs
});
});
Run Code Online (Sandbox Code Playgroud)
我怎么能从查询中返回结果,更像这样?
var a_users = users.find({}); //non-working example
Run Code Online (Sandbox Code Playgroud)
这样我就可以在页面上发布多个结果?
喜欢:
/* non working example */
var a_users = users.find({});
var a_articles = articles.find({});
res.render('profile/profile', {
users: a_users
, articles: a_articles
});
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
我有一些测试 - 即Supertest - 加载我的Express应用程序.这个应用程序创建一个Mongoose连接.我想知道如何从我的测试中检查该连接的状态.
在app.js
mongoose.connect(...)
Run Code Online (Sandbox Code Playgroud)
在test.js中
console.log(mongoose.connection.readyState);
Run Code Online (Sandbox Code Playgroud)
如何访问app.js连接?如果我在test.js中使用相同的参数进行连接,那么会创建一个新连接还是查找现有连接?
我相信这个问题与此类似,但术语不同.从Mongoose 4 文档:
我们也可以定义自己的自定义文档实例方法.
// define a schema
var animalSchema = new Schema({ name: String, type: String });
// assign a function to the "methods" object of our animalSchema
animalSchema.methods.findSimilarTypes = function (cb) {
return this.model('Animal').find({ type: this.type }, cb);
}
Run Code Online (Sandbox Code Playgroud)
现在我们所有的动物实例都有一个findSimilarTypes方法可用.
然后:
向模型添加静态方法也很简单.继续我们的animalSchema:
// assign a function to the "statics" object of our animalSchema
animalSchema.statics.findByName = function (name, cb) {
return this.find({ name: new RegExp(name, 'i') }, cb);
}
var Animal = mongoose.model('Animal', animalSchema);
Animal.findByName('fido', function …Run Code Online (Sandbox Code Playgroud) 我正在使用Mongoose.js来创建带有模式的模型.
我有一个模型列表(很多),有时我想获得构成特定模型的属性/键.
是否有方法为任何给定的模型提取属性模式?
例如,
var mySchema = module.exports = new Schema({
SID: {
type: Schema.Types.ObjectId
//, required: true
},
teams: {
type: [String]
},
hats: [{
val: String,
dt: Date
}],
shields: [{
val: String,
dt: Date
}],
shoes: [{
val: String,
dt: Date
}]
}
Run Code Online (Sandbox Code Playgroud)
);
是否可以提取/识别模式的属性[SID, hats, teams, shields, shoes]?
在mongoHQ和mongoose上使用node.js,mongodb.我正在为类别设置架构.我想使用文档ObjectId作为我的categoryId.
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Schema_Category = new Schema({
categoryId : ObjectId,
title : String,
sortIndex : String
});
Run Code Online (Sandbox Code Playgroud)
然后我跑了
var Category = mongoose.model('Schema_Category');
var category = new Category();
category.title = "Bicycles";
category.sortIndex = "3";
category.save(function(err) {
if (err) { throw err; }
console.log('saved');
mongoose.disconnect();
});
Run Code Online (Sandbox Code Playgroud)
请注意,我没有为categoryId提供值.我假设mongoose将使用模式生成它,但文档具有通常的"_id"而不是"categoryId".我究竟做错了什么?