Tag: mongodb

Express Async – 发送后无法设置标题,双重callback?

我有一个恼人的地狱问题与'错误:发送后无法设置标头。 在Express中。 这个代码最初使用的是Restify而不是Express,而我一直以来以这种或那种forms出现这个问题。 该代码向另一个API(已编辑)发出2个asynchronous请求,并将结果合并到单个JSON中,然后将其存储到MongoDB中。 任何洞察力将不胜感激,因为我尝试了所有我能想到的,不知道为什么相同的function将在Restify中起作用,而不是Express在适当的变化。 请不要评论厄运金字塔,我知道这不是理想的,但这不是重点;) app.post('/rest/test', jsonParser, /*keycloak.protect(),*/ function (req, res, next) { var requestObj = req.body; try { /* run async requests in parallel */ async.parallel([ function asyncReqV3(callback) { request.post(reqUrl1, option_v3, function(err, response, body) { if(err) { console.log(err); callback(true); return; } else { callback(false, body); } }); }, /* v4 async request */ […]

NodeJS MongoDB连接string问题

我是使用MongoDB的新手,并希望在我的NodeJS应用程序中复制下面的命令。 mongo –host dds-xxxx.mongodb.rds.aliyuncs.com:3717 -u root -p password –authenticationDatabase admin 我需要能够从Node连接这些证书,最好但不一定使用MongoJS库。 任何援助将不胜感激! 再次感谢。

Mongoose-如何引用embedded文档元素?

我有users.js模式与embedded式文档数组宠物。 对于每个用户,一个用户可以有多个宠物(通常不会超过3个,我会认为)。 每只宠物都会有一个日常图表。 所以这是一个宠物的日常图表。 我已经阅读embedded式文件,每个数组元素被索引。 在daily.js中,如何引用它将属于populate()函数的宠物? var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema = new Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, username: { type: String, required: true, unique: true }, location: String, pets: [{ name: 'string', animalType: 'string'}], //could have more than one […]

将范围传递给db.collection.group

是否有可能传递一个全局上下文db.collection.group的keyf ? {MongoClient} = require 'mongodb' GLOBAL_VAR = 1 reducer = (obj, prev) -> prev.count += GLOBAL_VAR initial = {count:0} MongoClient.connect 'mongodb://localhost:27017/test', (err, db) -> throw err if err db.collection('orders').group [], {}, initial, reducer, (err, result) -> throw err if err console.log result 我得到以下错误 MongoError: exception: ReferenceError: GLOBAL_VAR is not defined at $group reduce setup:2

虚拟在Mongoose对象是未定义的?

我最近一直在学习MongoDB和Mongoose以学习MEAN栈。 使用这个课程: https : //www.edx.org/course/introduction-mongodb-using-mean-stack-mongodbx-m101x我试图为我的产品架构创build一个虚拟,显示价格更方便用户办法。 但是,打印出这个虚拟displayPrice它会出现未定义,除非通过toObject或toJSON访问,即使这样USD符号显示为一个问号。 我为任何愚蠢和明显的忽视道歉,我是这个数据库的新东西,无法find许多简单的解释事情的教程。 这里是代码: var mongoose = require("mongoose"); var Category = require("./categoryschema.js"); var productSchema = { name: { type: String, required: true }, // Pictures must start with http://" pictures: [{ type: String, match: /^http:\/\//i }], price: { amount: { type: Number, required: true }, currency: { type: String, enum: ["USD", […]

多语言NodeJs API

我正在用swagger和mongoDB在nodejs中开发一个REST API。 我正在寻找一个模块或一个良好的做法,以帮助我的多语言支持。 每个collections品都应该有自己的翻译,我不确定最好的办法。 我看了几个模块,但我发现的一切都是为了静态数据。 翻译由用户决定,可以随时以任何语言发布。 我需要确保用户能够以他发布的每种语言检索他的数据。 这里是我想要做的一个例子,但我不确定这是一个推荐的做法。 集合:公寓 英文资料: var apartment = { "id": "1×213" "name": "Flat 1", "price": 100000, "rooms": 2, "city": "paris", "country": "france", "zipcode": 75016, "address": "11 avenue du general de Gaulles", "description": "A wonderful apartment at 2 minutes from the eiffel tower" } 法国的数据: var apartment = { "id": "1×214" […]

mongo发现里面航行本地不工作

我正在尝试直接在mongo上使用sails本地适配器进行查询。 尽pipedb中有文档,但是我没有得到任何结果。 直接水线查找function返回文件。 不过,我想使用本机查找其他目的,并试图使其工作。 有什么build议么? 下面的输出:null []。 User.native(function(e,collection){ collection.find({phoneNumber:mPhoneNumber}).toArray(function(e,r){console.log(e,r);});

找不到mongodb集合/数据

我正在用node / express / mongodb使用mongoose在web应用上工作。 我在我的项目中创build了一个名为data的文件夹,在里面我创build了另一个名为db文件 数据 D b 当我启动mongo时,我使用–dbpath参数并将其指向我创build的项目data文件夹。 它连接好,一切。 我一直在向数据库写入一些数据,并且应用程序正确加载它,但是当我打开mongo shell时找不到数据。 首先是,当我打开壳我唯一的数据库是test 。 如果我没有指定数据库名称,这是自动创build的? 我如何指定数据库名称? 我有我的app.js文件中的以下内容: var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/abduldb'); 我的猜测是应该创build一个名为abduldb的数据库是否正确? 我在mongo shell中看不到这个。 此外,当使用test数据库,我运行show collections ,并显示没有结果…我的数据写在哪里? 我现在只有一个型号,看起来像这样: var mongoose = require('mongoose'); var ItemSchema = new mongoose.Schema({ title: String, body: String }); mongoose.model('item', ItemSchema); 我正在添加新的项目在我的路线文件使用: router.post('/items', function (req, res, next) { var item […]

Mongoose.js在数据库中find昨天的date

我想查find昨天为止创build的所有用户。 这是我的代码,使查询string: var today = new Date(); var a = today.getDate(); a–; today.setDate(a); var yesterday = today.toDateString(); 它会返回类似于:Sun Jan 17 2016 …哪个是昨天的date,但是db以iso格式存储date,如: "date" : ISODate("2016-01-13T13:23:08.419Z")我得到的是2016-01-13T13:23:08.419Z 。 我现在的问题是,我不能使用昨天的variables来查询数据库为我需要的用户,即使我可以,我不知道如何find每一个登记,包括今天发生的。 任何帮助? 非常感谢你!

我如何在node.js中构造一个特定的查询来从mongodb中获取数据?

我正在使用mogoosejs并有一个数据模型: var UserSchema = new Schema({ username: {type: String, required: true}, location: {type: [Number], required: true}, // [Long, Lat] created_at: {type: Date, default: Date.now}, updated_at: {type: Date, default: Date.now} }); 现在在这个模式上,我想执行一个查询来查找某个区域内的所有用户,所以我做了如下的操作: var query = User.find({}); if(distance){ query = query.where('location').near({ center: {type: 'Point', coordinates: [long, lat]}, maxDistance: distance * 1609.34, spherical: true}); } 所以这工作得很好,但现在我想添加另一个参数到这个查询 – date。 […]