Tag: mongoose

mongodb mongoose忽略来自查询的当前用户

我正在编辑我的网站的编辑用户configuration文件页面。 当用户编辑他们的用户名并按保存,我想先检查,看看是否有其他用户首先有这个用户名。 User.findOne({'username' : newUsername }, function(err, user){ if (!user){ //no user exists with this name //code for changing current user's name to newUsername } else { //a user exists with this name //code for returning to the edit page, and displaying error message to user } }); 问题是当我离开用户名相同(并编辑一个不同的值,如电子邮件)。 当它查询用户名时,它在mongodb上拉我的文档,因此它不会改变任何东西(因为用户存在!)。 我怎样才能把一个特定的文件完全从查询中删除?

等待Mongoose查询完成

说我有以下代码: function MyClass(id, x, y, z) { var self = this; MyModel.findByID(id, function(err, model) { if(err) { // Do something } self.model = model }); this.x = x; this.y = y; this.z = z; } MyClass.prototype.myMethod = function() { console.log(this.model.id); } var obj = new MyClass(123, 7, 8, 9); obj.myMethod(); 如何确保this.model在myMethod设置,因为Mongoose查询是asynchronous的。 还是我用“非”NodeJS方式编程? 谢谢

我如何检索所有属性是给定参数的子string的文档?

我有以下mongoose纲要 var People= new Schema({ firstName: String, alias: [String] }); 我正在寻找一种方法来返回其中一个aliasstring匹配或作为一个给定值的子string存在的所有文档。 例如: { firstName: Jon alias: ['foo', 'fuchs']} { firstName: Ann alias: ['bar', 'fuchsbar']} { firstName: Paul alias: ['foobar']} 如果我通过foob ,我想回到乔恩。 如果我通过foobar ,我想回到乔恩,安和保罗。 如果我通过fuchs ,我想回到Jon。 在Mongo中可以做这样的子string查询吗?

从mongoose数据库获取模型

我目前正在考虑build立一个小的基于REST的服务,我可以将一些数据发布到一个mongoose数据库并GET数据。 这是我的main.js文件: var http = require("http"); var DAO = require("./DAO"); var express = require("express"); var util = require('util'); var app = express(); app.use(express.bodyParser()); app.post('/postIsles',function(req,res){ DAO[req.method](req.body); res.send("body" + req.body.name); }); app.get('/getIsles',function(req,res){ var isleVar = DAO[req.method](req); res.send(isleVar); }); app.listen("3000"); console.log("\nApp available at http://127.0.0.1:3000\n"); 和DAO.js: var mongoose = require('mongoose'); //Connect to database mongoose.connect( 'mongodb://127.0.0.1:27017/library_database' ); //Schemas var Isle […]

mongoose多租户 – 处理文档引用+填充

我读过一堆关于Mongoose / MongoDB中的多租户设置选项。 选项是: 分开数据库。 由于每个数据库实例的开销,这是一个“否” 同一数据库中的前缀集合 – 我将拥有共享相同模式的tenant1_Products和tenant2_Products 。 带有参考文件的标准馆藏。 看起来#2是缩放的最佳select,因为它允许通过前缀进行简单的分片。 所以,假设,我会实现这样的dynamic检索某个租户的模型: tenant.methods.getModel = function(name) { return mongoose.model(this.uniqid + '_' + name); } 假设我提前为所有租户注册所有模型,并使用正确的模式,那么我可以做var productModel = myTenant.getModel('Product'); ,然后从那里创build/更新。 这个问题是当你需要实现文档引用来使用Mongoose的populate方法。 例如, var productSchema = new mongoose.Schema({ name:String, categories:[ { type:mongoose.Schema.Types.ObjectId, // This will need to change to "tenant1_Category", etc ref:'Category' } ] }); mongoose.model('tenant1_Product', productSchema); […]

在填充时连接一个string

这里是我的代码来填充专辑和他们的图像: server.get(url_prefix + '/image', function(req, res, next) { var album_id = req.query.album_id Album.findOne( {_id : album_id} ) .populate('images') .exec(function (err, album) { if (album) { return res.send({ status: { error: 0, message: "Successful" }, data: { album: album } }) } else return notify_error(res, "No Results", 1, 404) }) }) 相册架构: var AlbumSchema = new […]

Mongoose嵌套模式validation不起作用

我有以下mongoose模型: RequestSchema = mongoose.Schema( merchant: type: String required: true products: [ 'Product' ] ) Request = mongoose.model('Request', RequestSchema) ProductSchema = mongoose.Schema( _type: String product_id: type: String required: true quantity: type: String required: true ) Product = mongoose.model('Product', ProductSchema) 但是,当我通过下面的请求(这是缺less一个数量),它得到validation,并保存到数据库: {"merchant": "ABC", "products":[{"product_id":"12345"}]} 但是,当我尝试删除商家时,尝试保存它会导致架构validation错误: {"products":[{"product_id":"12345", "quantity": "1"}]} 我如何使这些都返回validation错误?

保存在mongoose参考列表

我有以下模式: var TagSchema = new Schema({ name: { type: String, default: '' } }); var SnippetSchema = new Schema({ title: { type: String, default: '' }, tags: [{ type: Schema.ObjectId, ref: 'Tag' }] }); 我创build了一个新的Snippet并search一个标签(从请求中获取标签),将其添加到标签列表中。 var snippet = new Snippet(req.body); Tag.findById(req.body.tagid).exec(function(err, tag) { snippet.tags.push(tag); snippet.save(function(err) { if (err) { } else { res.json(snippet); } }); […]

NodeJScallback:跟踪数据库调用,所以我可以终止进程

这个月我从Heroku收到一个账单,这令我感到意外。 幸运的是,只有几美元,但我认为我的使用率并不高。 我查了一下账单,说上个月我用了大约1000个小时。 我简单地感到困惑,因为我的应用程序每小时运行几秒钟发送一些电子邮件,但后来我意识到这个过程并没有终止。 在注释掉我的代码之后,我确定该进程不会因为我的mongoose数据库连接仍然打开而退出。 但是我有几个嵌套的callback数据库,然后mailgun发送这些电子邮件,有时mailguncallback有自己的mailguncallback。 我如何跟踪这些数据并确保数据库在最后closures?

如何知道upsert是否成功,如果我能得到_id更新/插入的新文件

我正在使用这个代码来通过mongoose补充function到Mongo服务器 update({'name' : 'Jaki'}, {'name' : 'Charley', 'gender' : 'f'}, {upsert: true}) 它的工作原理,但如何知道操作是否成功,如果我可以得到_id更新/新插入的文件? (在会话flush()后sqlalchemy我可以得到更新/插入orm的ID)