Tag: mongodb

如何传递variables来聚合(匹配)与Mongoose和NodeJS?

我有这样的文件: { "_id" : 1, "time": { "start": ISODate("2016-01-18T00:12:41.000Z"), "end": ISODate("2016-01-18T05:12:41.000Z") }, "subject":"javascript", "class": "ABC" } … 我有客户端/服务器应用程序,客户端将发送请求到服务器有四个variables: timeS , timeE , sub , class 。 他们可能是空的string。 在我的服务器中,如何编写聚合查询来查找匹配过滤条件的logging? 我想忽略空的search项。 var query = db.collection.aggregate([ {"$match": {"$and":[]}} ]); 例: 如果timeS不是空的,我想$match $eq 。 如果timeS和timeE都不是空的,我想$match $gte:timeS, $lte:timeE 。

如何用Monk检索MongoDb集合标识

我试图用swig做一个非常简单的应用程序“todo应用程序”koajs,我已经成功地将数据插入Mongodb,在我的情况下,数据库的名称是'mytodos',收集是'todos'。 如何检索文档的“_id”? 我试过这样做: index.html(这列出了待办事项) <div class="todoListClass"> <p> <strong>Your todo list:</strong> {{todos.length}} {% for todo in todos%} <li>{{todo.text1}} <span> <a href="/todo/delete/{{todo._id}}">Delete</a></span></li> {%endfor%} </p> </div> 但我似乎无法得到我想要的结果。 它只是返回“[Object] [object]”。

议程计划的工作只运行一次

我有一个示例程序来开始使用议程,并在第一次运行后卡住。 var Agenda = require('agenda'); var agenda = new Agenda({db: {address: 'localhost/sample-dev', collection: 'pollingJob'}}); agenda.define('First1', function(job, done) { var d = new Date(); console.log("Hello First Job at " + d ); }); agenda.on('ready', function() { agenda.every('5 seconds', 'First1'); agenda.start(); }); 我已经按照议程上的指示完成了这一切,我在这里错过了什么?

计数未定义与MongoDB和Nodejs

问题 Node.js MongoDB库始终返回未定义的collection.count({}) 。 这个问题已经发布和回答了很多次,我一直坚信所有以前的解决scheme,但是没有一个能够工作,而且我总是不确定。 作为问题的背景,我正在创build一个Job Automator,并且在添加一个新的作业之前,我想确保数据库中已经存在的0个logging与正在添加的新作业名称相同(即名称是唯一的)。 编辑 :有几个我想要允许重复的情况下,所以我不想在数据库级别使用索引和dis-allow重复。 码 在这种情况下, console.log()内部count只是打印未定义。 在这种情况下,我将一个空的查询string作为debugging的一部分(目前不testing名称冲突)。 add: function(io, newJob) { //mc is where require('mongodb').MongoClient is saved //connectionString contains a valid connection string //activeCollection contains the name of the collection //(I am sure mc, connectionString and activeCollection are valid) //I know this as I have used them to insert […]

如何检查是否有使用mongoose现有的MongoDB连接?

我有一个快速的应用程序,通过mongoose连接到MongoDB,我也有一个初始化脚本,偶尔会连接到MongoDB。 有没有什么办法可以检测到数据库是否存在连接,所以我不需要在脚本中再次连接就可以运行它,也可以通过mongoose多次连接到数据库。

从GridFs(MongoDB)显示图像

我正在使用下面的代码来读取使用GridFs保存在MongoDB中的图像文件: app.get('/picture', function(req, res) { var readstream = gfs.createReadStream({ filename: 'trooper.jpeg' }); readstream.on('data', function (data) { // We got a buffer of data… var buf2 = new Buffer(data).toString('base64'); res.send(buf2.toString()) console.log(buf2.toString()); console.log(data); }); readstream.on('end', function () { // File finished reading… }); }); console.log(buff.toString())的输出 是: dHJvb3Blci5qcGVn console.log(data)的输出 是: <Buffer 74 72 6f 6f 70 65 72 […]

如何在单个mongo查询中将字段的值分配给另一个字段

假设你有一个mongologging: { _id : 1234, x: 12, y: 34 } 我需要一个查询,以这样的方式更新一个特定的_id x = y;//assign the value of y to x y = new Date().getTime();//assign current time to y 对于特定的_id.current.currentCurrently我正在读取修改实体和更新它的id。这是可能做到这一点在一个单一的查询? 使用mongoose会是什么样子?

select字段的值作为数组mongoose / mongodb聚合

我有一个mongo查询 db.memberships.aggregate([{ '$match': { unit: new ObjectId('566fbaa1e63225b10bacac44') } }, { '$lookup': { from: 'seasons', localField: 'season', foreignField: '_id', as: 'season' } }, { '$unwind': '$season' }, { '$project': { season: { 'name': '$season.name', 'id': '$season._id' } } }, { '$group': { _id: '$_id', 'season': { '$addToSet': '$season' } } }, ]); 哪个结果 [ { "_id": […]

如何转换多个Mongoose文档?

我的每个模式都有一个方法,称为toItem() ,它将文档转换为更加冗长/可读的forms。 我如何创build一个toItems()方法来为文档数组做同样的事情? 我的示例架构: var mongoose = require('mongoose'); var membershipSchema = new mongoose.Schema({ m : { type: mongoose.Schema.ObjectId, ref: 'member' }, b : { type: Date, required: true }, e : { type: Date }, a : { type: Boolean, required: true } }); var accountSchema = new mongoose.Schema({ n : { type: String, trim: […]

数据没有存储在Mongodb中

我试图在NodeJS服务器上运行这个代码,并从Postman发出'POST'请求。 我不断得到一个响应,当它应该填充数据时变成一个空的data字段。 这是“POST”请求后服务器的响应: { data: [] } data数组不能为空。 var deviceModel = new Schema({ data: [ { type: Schema.Types.Mixed, required: true} ], time: { type: Date, default:Date.now } }); 这是主应用程序文件的内容: var app = express(); app.use(bodyParser.urlencoded({extended:true})); //app.use(bodyParser.json()); var deviceData = mongoose.model('device1', deviceModel); app.post('/',function(req, res){ var devicedataat = new deviceData(req.body); console.log(req.body); devicedataat.save(); console.log(devicedataat); res.status(201); res.send(devicedataat); }); 我想知道我在做什么错,我怎样才能解决这个问题。