Tag: mongodb

在node.js环境中将mongodb与elasticsearch集成在一起

我正在做一个有如下设置的项目: 我有一个Amazon EC2集群,有一个主服务器,3个configuration服务器和3个分片服务器。 Master有一个node.js应用程序,它基本上是一个使用Express.js模块编写的REST API。 我正在使用MongoDB作为数据库。 Master有“mongos”服务运行,将数据分割成3个分片服务器。 这些服务器上运行着“mongod”服务。 有了这个设置,我想整合elasticsearch来执行search查询。 为了做到这一点,我想在我的node.js REST API应用程序中添加一个path,以对存储在分片中的数据执行search查询。 考虑到我在独立机器上运行三个碎片,是否还有其他步骤? 如何configurationelasticsearch访问分片中的数据来构build索引? 它会自动检测这个configuration并build立索引吗? 有人可以给我提供我应该遵循的步骤来完成这个步骤吗?

在使用`$ unwind`的时候,我得到了一个数组的文档。 如何让它们像数组一样?

这是我的模式 { "productid": {type: mongoose.Schema.Types.ObjectId, ref: 'products', required: true}, "itemcode": {type: String, required: true}, "itemname": {type: String, required: true/*, index: "text"*/}, "itemdescription": {type: String, required: true}, "itemimgurl": {type: String, required: true}, "mainprice": {type: Number}, "offerprice": {type: Number, required: true}, "unit": {type: String, required: true}, "offertag": {type: String}, "itemprice":[ { "itemname" :{type: String, required: true}, "unit": […]

在嵌套数组上更新mongoDb集合

我有这样的JSON。 我想添加一个代理技术departmentName就像它是如何支持。 我一直试图与$push并添加设置,但没有帮助。 这是我曾经试过的: var depName = "Support"; // i get this from a form db.collection('departments').update( { "departments.departmentName": depName }, { $addToSet: { "departmentName": depName, "agents": { "agentName": "xxx", "agentNumber": "xxx" } } }, function(err,result) { } ); 和这个 db.collection('departments').update( { "departments.departmentName": depName }, { $addToSet: { "agents": { "agentName": "xxx", "agentNumber": "xxx" } } […]

Sails.js + Mongodb使用ORM插入一个嵌套的jsonlogging

我在我的Sails设置中使用MongoDB来存储我的集合。 我通过一个API发出一个POST请求,其请求体看起来像这样: **THE REQUEST BODY** { "name":"Martin", "phone":"5447874787", "comment":"This is a comment", "campaign":{ "campaign_id":123454587, "name":"Digital Marketing", "category":"Marketing", "sub_category":"Digital", "product_name":"no product", "product_id":5417 } } 这个logging将存储在我的名为“leads”的 mongodb集合中。 我的“潜在顾客”模型如下所示: module.exports = { schema: true, attributes:{ name:{ required:true, type:"string", unique: true }, phone:{ type:"string" }, comment:{ type:"string" }, campaign:{ model: 'lead_campaign_detail', // via: "lead_model" } } }; 相关的“Lead_campaign_detail”模型如下 module.exports […]

如何使用Node.jsbuild立到MongoDB数据库的SSH隧道连接

我的凭据与Robomongo完美协作,但我无法与node.jsbuild立连接 我试图使用ssh2和tunnel-ssh npm模块进行连接,并且两次都失败。 – mongo连接不需要密码 -sh连接是用pem键进行的 这是我使用ssh2模块的代码,我可以正确build立隧道,但是mongo连接失败 var Client = require('ssh2').Client; var conn = new Client(); conn.on('ready', function() { console.log('Client :: ready'); //mongo connection mongoose.connect('mongodb://localhost:27000/'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { console.log("database connection established"); var users = db.collection('user'); var getallUsers = function (date, callback){ users.find({}).toArray(function(err,data){ callback(data); }) }; getallUsers(null, function […]

使用passport.js LocalStrategy进行一个字段validation

我正在使用passport.js LocalStrategy来validation用户名和密码。 但现在我有一个字段来validation是active: true/false (无论用户是否激活)。 下面的代码显示了用户名和密码的validation。 app.js passport.use(new LocalStrategy({ usernameField: 'username', passwordField: 'password'}, function(username, password, done) { User.findOne({ name : username}, function(err, user) { if (!user){ return done(null, false,{message: 'Incorrect username' }); } if(user){ var validPassword = user.comparePassword(password); if(!validPassword){ return done(null, false,{message: 'Incorrect password' }); } } return done(null, user); }); } )); router.post('/pages/auth/login', function(req, […]

Mongoose不会从JSON数组创build子文档

我正在尝试编写一个包含一级数据和数组的MongoDB的JSON对象。 会发生什么反而是所有的第一级数据被存储,但是包含在数组中的任何东西都不是。 当logging服务器收到的数据时,我看到整个对象,这导致我相信我的Mongoose代码有问题。 所以例如,如果我发送这样的东西: issueId: "test1", issueTitle: "testtest", rows: [ {order:1,data: [object]}, {order:2,data: [object]}, ] 只有以下内容被存储: issueId: "test1", issueTitle: "testtest", lastUpdated: Date, 我有以下Mongo的模式: //model.js var mongoose = require('mongoose'); var model = mongoose.Schema({ issueId : String, issueTitle : String, lastUpdated : {type: Date, default : Date.now}, rows : [{ order : Number, data : [ { […]

当db命令不能执行时,Mongoose不报告错误

我正在testing的Mongooseerror handling的情况下db命令(保存,更新等)不能执行连接到数据库的连接之前,或执行数据库命令执行期间。 在这个示例代码中: var myModel = new MyModel(data); myModel.save(function (err, newDoc, numAffected) { if (err || numAffected === 0) { logger.error('Error inserting message: ' + err); } logger.info('Message inserted'); }) 如果我在myModel.save()行上放置一个断点,杀死mongod进程,然后继续执行代码,没有任何反应,mongoose似乎无声无息地失败。 callback从不被调用,并且在这里或者在app.js中定义如下的连接“错误”事件监听器中没有任何logging: var db = mongoose.connection; db.on('error', function(e) { logger.error('connection error:' + e); }); 我会期望一些错误消息告诉我保存不能执行。 任何意见,我在做什么错了? 我使用的是MongoDB 3.2,mongoose 4.5.1,express 4.14和node 4.2.1。 提前致谢, 弗雷德

如何获得具有所有匹配logging的特定数字字段的总和

即时通讯使用与node.jsmongoose和我有交易模式和发票模型。 我想获得具有特定发票编号的所有logging。 即时通讯使用此查询来获得发票匹配的交易。 Transaction.find({invoice_id: invoiceId}) 即时通讯logging在这种forms [ { "_id": "5795e3f4f4a0fb8c1dff20ad", "description": "This is a transcation", "invoice_id": "5795db00bfa9d366194a454d", "amount": 50 }, { "_id": "5795e3faf4a0fb8c1dff20ae", "description": "This is a transcation", "invoice_id": "5795db00bfa9d366194a454d", "amount": 100 } ] 但问题是,我也想通过总和“金额”字段值在事务数组的每个对象的totalAmount。 我期望的结果是 [ { "_id": "5795e3f4f4a0fb8c1dff20ad", "description": "This is a transcation", "invoice_id": "5795db00bfa9d366194a454d", "amount": 50 }, { "_id": "5795e3faf4a0fb8c1dff20ae", "description": "This […]

使用节点向第三方Apis节stream请求

所以我正在build立一个空间地图并把关系转换成一个mongodb。 在这种情况下,我有地区,星座和太阳系。 一个地区可以有多个星座,每个星座可以有多个太阳系。 我有一个允许抓取一些这样的API,但它需要每个项目的API调用。 带来我正在做的问题~6000每天通话。 下面是第三方API标准: 通用速率限制:每秒150个请求 突发尺寸:400 并发连接:20 这里是我的db模型: 地区型号: var mongoose = require('mongoose'); var Schema = mongoose.Schema; //Schema Definition var regionSchema = new Schema({ _id: Number, name: String, description: String, href: String, constellations: [{ type: Schema.ObjectId, ref: 'Constellation' }]//Reference }); //Expose (export) the model module.exports = mongoose.model('Region', regionSchema); 星座模式: var mongoose = require('mongoose'); […]