Tag: mongoose

用mongoose创build独特的自动增量场

给定一个Schema: var EventSchema = new Schema({ id: { // … }, name: { type: String }, }); 我想使id独特和自动增量。 我试图实现MongoDB的实现,但有理解如何在mongoose正确的问题。 我的问题是 :在没有使用任何插件的情况下,在mongoose中实现自动增加字段的正确方法是什么?

node.js&express – 应用程序结构的全局模块和最佳实践

我正在构build一个node.js应用程序,它是一个REST api,使用express和mongoose作为我的mongodb。 我已经得到了所有的CRUD端点,但是我只是想知道两件事情。 如何在这种路由上进行扩展,特别是如何在路由之间共享模块。 我希望我的每一个路线都进入一个新的文件,但显然只有一个数据库连接,你可以看到我已经在people.js的顶部包括mongoose。 我必须在我的people.js中写出三次模型的模式吗? 第一个模式定义模型,然后列出createPerson和updatePerson函数中的所有variables。 这感觉就像我在一天后做了php / mysql CRUD。 对于更新function,我试过编写一个循环来通过“p”循环来自动检测要更新的字段,但无济于事。 任何提示或build议将是伟大的。 另外,我会对整个应用程序有任何意见,对节点来说是新的,很难知道你做事的方式是最有效的或“最佳”的做法。 谢谢! app.js // Node Modules var express = require('express'); app = express(); app.port = 3000; // Routes var people = require('./routes/people'); /* var locations = require('./routes/locations'); var menus = require('./routes/menus'); var products = require('./routes/products'); */ // Node Configure app.configure(function(){ app.use(express.bodyParser()); app.use(app.router); […]

从URL查询string直接提供的mongo查询有多危险?

我正在玩node.js , express和mongoose 。 为了使事情正常运行,我将Express查询string对象直接传递给了一个mongoose查找函数。 我感到好奇的是这种做法在现场应用程序中有多危险。 我知道一个RDBMS将非常容易被SQL注入。 除了“清理你的投入”的好build议,这个代码有多恶心: app.get('/query', function (req, res) { models.findDocs(req.query, function (err, docs) { res.send(docs); }); }); 这意味着aa获取请求到http://localhost:8080/query?name=ahsteele&status=a只会将以下内容插入到findDocs函数中: { name: 'ahsteele', status: 'a' } 这有很多原因会让人觉得不舒服,但这有多危险呢? 将查询parameter passing给mongodb的最佳做法是什么? 快递是否提供任何现成的卫生处理?

使mongoose.js查询同步运行

我有两个mongoosecollections。 第一个存储地点列表,第二个是访问地点。 我的节点代码通过并试图获取每个地方的访问列表,并构build一个string,我输出为JSON。 第一个查询在第二个开始之前完成 – 是否有办法使它们同步运行?

是否有可能从mongoose的文件中获取模型?

var UserSchema = new Schema({…}); // Schema var User = mongoose.Model('User', UserSchema); // Model var user = new User({…}); // Document 只给出文档(在这种情况下为用户),是否有一种简单的方法来获取模型(在这种情况下是用户),而不需要事先知道该文档引用什么模型? 有一个user.schema,但据我所知,没有user.model。 上下文被赋予一个文档和一个path,我想知道是否有其他对象在数据库中具有相同的值的path。 谢谢。

mongoose模式可选字段

我有一个像这样在nodejs中有mongoose的用户模式 userschema = mongoose.Schema({ org: String, username: String, fullname: String, password: String, email: String }); 除了有时我需要添加更多的字段。 主要问题是:我可以在monogoose模式中有可选字段吗?

将“虚拟”variables添加到mongoose模式?

我有以下文件架构: var pageSchema = new Schema({ name: String , desc: String , url: String }) 现在,在我的应用程序,我想也有对象内的页面的HTML源,但我不想将其存储在数据库。 我应该创build一个“本地”增强对象,它有一个对数据库文件的引用? function Page (docModel, html) { this._docModel = docModel this._html = html } 有没有办法通过添加一个“虚拟”字段直接使用文档模型?

尝试在Webpack上使用带有node-webkit目标的mongoose

我使用webpack并将其作为node-webkit的构build输出。 我想要求我的项目中的mongoose模块连接到mongodb,但是当我尝试打包项目时,它总是说明一些错误: WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js Module not found: Error: Cannot resolve 'file' or 'directory' ./win32/x64/bson in /Users/Johannes/Documents/Development/holmes/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/node_modules/bson-ext/ext @ ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js 6:10-37 WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js Module not found: Error: Cannot resolve 'file' or 'directory' ./win32/ia32/bson in /Users/Johannes/Documents/Development/holmes/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/node_modules/bson-ext/ext @ ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js 8:10-38 WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js Module not found: Error: Cannot resolve 'file' or 'directory' ../build/Release/bson in /Users/Johannes/Documents/Development/holmes/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/bson/node_modules/bson-ext/ext @ ./~/mongoose/~/mongodb/~/mongodb-core/~/bson/~/bson-ext/ext/index.js […]

mongoose预存和validation的区别? 何时使用哪一个?

目前我正在使用pre('save')来进行validation: UserSchema.pre('save', true, function(next, done) { var self = this //in case inside a callback var msg = helper.validation.user.username(self.username) if (msg) { self.invalidate('username', msg) done(helper.getValidationError(msg)) } else done() next() }) 助手模块具有接受input并返回错误消息的function。 exports.user = { username: function(input) { if (!input) return 'username is required' var min = 3 var max = 10 if (input.length < min) […]

mongoose:填充填充字段

我使用MongoDB作为我的应用程序的日志pipe理器,然后同步移动客户端。 我在NodeJS中设置了这个模型: var UserArticle = new Schema({ date: { type: Number, default: Math.round((new Date()).getTime() / 1000) }, //Timestamp! user: [{type: Schema.ObjectId, ref: "User"}], article: [{type: Schema.ObjectId, ref: "Article"}], place: Number, read: Number, starred: Number, source: String }); mongoose.model("UserArticle",UserArticle); var Log = new Schema({ user: [{type: Schema.ObjectId, ref: "User"}], action: Number, // O => Insert, 1 […]