Tag: mongodb

使用angular2 +服务编辑对象,mongoDB和NodeJs

我想对存储在数据库(mongoDB)中的数据进行基本编辑。 这是我所做的: angular度服务 createReview(review) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers }) .subscribe(res => { console.log(res.json()); }); } editReview(review) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://localhost:3000/api/reviews', JSON.stringify(review), { headers: headers }) .subscribe(res => { console.log(res.json()); }); } deleteReview(id){ this.http.delete('http://localhost:3000/api/reviews/' + id).subscribe((res) => { console.log(res.json()); }); } 在server.js中 […]

聚合中的mongoose模式方法

怎样才能在聚合中访问方法? let mongoose = require("mongoose"), Schema = mongoose.Schema; let UserSchema = new Schema({ name: String, lastname: String }); UserSchema.methods.fullname = () => { return this.name + " " + this.lastname; }; let UserModel = mongoose.model("user",UserSchema); let query = [ { $match: { "name": /foo/i } } ]; UserModel.aggregate(query, (err, users) => { if(err) throw new […]

从MongoDB读取大数据并显示在图表中

我目前在mongoDB中有大约100,000个文件。 这些数据包含一组数据点,我想在浏览器中使用Node.js在graphics中进行可视化。 目前,当我尝试读取所有数据时,出现“堆内存不足”错误。 我正在使用chart.js来表示它。 我能够看到10K左右的logging,但这些logging也太慢了。 任何build议如何可以在Node.js有效地完成

如何使用findByIdAndUpdate将objectId推送到对象的mongodb模式字段数组

每当我打我的API我得到同样的错误 – 我曾尝试发送值作为参数也失败了。 任何帮助,将不胜感激。 当我使用$set每次调用Web服务时更新相同的值,但它确实工作,但不是与$push 。 MongoError:字段'songId'必须是一个数组,但在文档中是objectIdtypes的 {_id: ObjectId('59709590380026118c22dd61')} 我的播放列表模式代码: – var PlaylistSchema = new mongoose.Schema({ name: String, coverphoto: { type: String, default: '' }, userId: { type: ObjectId, ref: 'User' }, songId: [{ type: ObjectId, ref: 'Song' }], updated_at: { type: Date, default: Date.now }, }); 我的宋架构代码 var mongoose = require('mongoose'); var Schema = […]

连接节点应用程序与在Droplet托pipe的MongoDB

我现在试图在DigitalOcean上托pipe我的Angular,Node,MongoDB应用程序。 我成功地build立了两个Droplet,一个用于Node后端,一个用于MongoDB数据库。 到目前为止,我正在使用本地链接连接到本地数据库,如下面的链接mongodb://localhost:27017/creditApp 。 如何将我在Droplet中托pipe的Node应用程序连接到另一个Droplet中托pipe的MongoDB? 该数据库设置了pipe理员密码,也允许从ufw列表中的后端的IP地址。 我认为它需要一个configuration对象与pipe理员名称,密码,ssh密钥.pub文件等,并附加该对象,而试图连接它,但我不知道如何做到这一点。 我怎么做到这一点? 任何帮助将高度赞赏,在此先感谢。

Mongo根据计算的条件进行sorting

我有一个Mongo集合的消息,看起来像这样: { 'recipients': [], 'unRead': [], 'content': 'Text' } 收件人是用户ID的数组,而unRead是尚未打开邮件的所有用户的数组。 这是按预期工作的,但我需要查询所有消息的列表,以便返回前20个结果,首先优先处理未读的消息,如下所示: db.messages.find({recipients: {$elemMatch: userID} }) .sort({unRead: {$elemMatch: userID}}) .limit(20) 但是这不起作用。 根据是否符合某个标准,对结果进行优先sorting的最佳方法是什么?

node.js mongoose chai-http RESTful APItesting(唯一字段)的行为与手动ARC APItesting不同

各位开发者, 我编写了一个基于node.js和mongoose的RESTful API服务器。 在编写相应的testing时,我对使用chai-http进行自动化testing和使用ARC(高级REST客户端,chrome插件)的手动APItesting之间的明显不同之处有着奇怪的体验。 我的用户模型(见下面的定义)有一个唯一的“用户名”字段。 当我使用ARC使用现有的用户名login新用户时,我按照预期从mongodb驱动程序中获取E11000。 当我这样做(至less我这样认为)使用chai-http.request.post,我得到一个statuscode 200,一个SUCCESS结果,我有两个用户在我的testing数据库集合中具有相同的用户名! 用户模式模式,包括/挂钩/导出等。ommited: const userSchema = new Schema({ admin_info: AdminInfo, role: { type: String, required: true}, username: { type: String, required: true, unique: true, index: { unique: true } }, password: { type: String, required: true }, firstname: String, lastname: String, mail: String, }); 用于POST /用户的通用路由处理函数,其中“model”是上述用户模式的一个实例: function addEntry(req, res, […]

节点Mongo连接错误:连接4到xxx集群closures

我正在使用AWS Lambda – Nodejs开发应用程序,并使用Mongodb作为后端。 为了连接Mongodb,我使用了nodejs本地库mongodb 。 问题是,随机,Mongodb抛出错误“连接4到群集closures”。 我在Google上find了一些答案,但没有解决。 像一些要求添加keepalive等。如所build议的那样,当使用AWS Lambda时,保存数据库连接,我坚持这些准则。 这是连接到Mongodb的函数的片段。 var connectToDatabase = function(_callback){ var options ={ server: { socketOptions: {keepAlive: 1} }, poolSize:100, replset: { rs_name: 'voila-cluster-shard-0', socketOptions: {keepAlive: 1} } } MongoClient.connect(url,options,function(error,connection){ if(error){ console.log(error) _callback(error) } else{ client = connection _callback(null,client); } }) } 有人可以帮我在这里。

仅在mongoDB Node JS上存在集合时才显示给客户端

我有这个在Mongoose上定义的模式模型: var mongoose = require("mongoose"); var IngredientSchema = new mongoose.Schema({ name:String, number:Number, exist:Boolean, photoName:String }) module.exports = mongoose.model("Ingredient", IngredientSchema); 而且我想在网页上显示不同的结果,这取决于数据库中是否已经创build了一个成分。 这是我到目前为止(但它不起作用): <!– Check if inventory is empty or not, and display accordingly –> <% if ( ! ingredients) { %> <p>Add you first ingredient to the inventory !!</p> <% } else { %> <% ingredients.forEach(function(ingredient) […]

如何通过查询保留前N个文档并删除mongoDB中的所有其他文档?

我试图find一个最佳的方式来删除查找查询返回的顶部N以外的所有文档。 我们目前正在做的是得到所有的文档IDsorting和跳过N.然后发射删除查询为{id: {$in: idsList}} 。 1. model.find(findQuery).sort({_id:-1}).skip(N) -> collecting all ids from this query(idsList) 2. model.remove({_id:{'$in':idsList}}) 有没有办法使用单个查询以最佳方式做到这一点?