Tag: mongodb

用Angularjs从Mongodb获取数据

我来自LAMP堆栈背景,但最近我对Node.js / Angluarjs组合感兴趣。 现在我想要把Mongodbjoin到组合中,但是我正在用最困难的时间来build立它。 我发现每个教程都使用不同types的堆栈,其中一些使用Express.js,另外一些使用Mongoose或其他types。 我如何去使用angularjs HTTP服务连接到Mongo? 我是否需要像Mongoose这样的中间库,还是可以直接使用Angular? 作为一个实验,我已经设定了一段时间了。 我正在尝试在这个页面上做我正在做的事情。 在这里,我将数据存储在JSON文件中。 我想用Mongodb的连接replace那个JSON文件。 我有节点的全球安装的MongoDb,我已经通过控制台创build了一个数据收集。 你可以在这里看到我的实验 – http://monkbunker.com/saas/#/并且我已经为这个http连接的代码可以在这里看到从第14行开始https://github.com/seanandersonmke/saas/斑点/主/ JS / main.js 这与我如何使用MongoDB相似吗? 如何设置初始连接? 现在我已经坐了好几个小时了,每次都被打倒了。 请帮忙。 谢谢。

在variablesmongo db中使用variables作为键名

我正试图插入一堆数组到mongoDB中。 数据看起来像这样: var places = { city: ['london', 'york', 'liverpool'], country: ['uk', 'france'] }; 每次我添加一个数组,一些条目可能已经在那里,所以我想确保没有重复。 db.collection('places').ensureIndex({'city': 1}, {unique: true }, function(){ }); db.collection('places').ensureIndex({'country': 1}, {unique: true }, function(){ }); 然后,我需要通过位置对象循环,并添加每个项目: for(var key in places){ db.collection('places').update({_id: 1}, { $addToSet: { key: { $each places[key] } } }, function(err, docs){ if(err){ console.log(err); }else{ console.log('success'); } }); } […]

节点js module.exports模块参数

我在Node.js中是新的,我已经创build了一个基于mongoose的模块,但我有configurationfunction的问题。 其实我的模块使用mongoose.connect连接,但我想改变它,以使用mongoose.createConnection 。 为此,我创build了一个接受连接string作为参数的init函数。 我的问题是“我怎么module.exports连接到我的模块?” 例如: // server.js var mymodule = require('mymodule'); mymodule.init('http://localhost:27017/test'); // mymodule/index.js var mongoose = require('mongoose'); module.exports = { init: function(connString){ module.exports = connection = mongoose.createConnection(connString); // this is wrong } } // mymodule/user.js // I want to use the connection to create the mongoose 'User' model var mongoose = require('mongoose'); var […]

meteor的非实时Web应用程序

我一直在评估Meteor的大部分非实时的Web应用程序,因为想要减less大部分与设备兼容性,JS压缩等有关的内务处理任务。 应用程序只有一个集合实时更新到UI,否则不需要与UI实时数据同步。 也将不得不整合应用程序到分片MongoDB(已经有很less的TB数据),Solr和Memcached。 是否有可能使用Meteor作为一种AJAX Web应用程序和非响应式(非实时)Web应用程序?

Nodejs Express sendStatus错误

Web应用程序使用快递服务器,nodejs作为语言,MongoDB作为数据库和mongoose作为包装。 Express在端口3000上运行服务器,我试图为一个集合实现一个基本的CRUD。 我以前用过restify,通常做一个res.send(406,“err – body”); 不会导致任何问题,但我似乎无法用明示的方式解决。 查看快速文档,res.status(404).send('Bad Request'); 是你如何发送一个自定义消息的状态代码。 它有效的时候有某种无效的input,但是当我试图保存到mongodb newSchema.save(function(err){ if (err) { res.status(500).send('error saving'); } else { res.sendStatus(200); } }); 并调用res.sendStats(200); 它给了我错误:错误:发送后无法设置标题。 使用httprequester时,错误是404,不能POST / user / create。

同时填充多个字段

我有一个模式称为聊天: var ChatSchema = new Schema({ vehicle: { type: Schema.Types.ObjectId, ref: 'Vehicle' }, messages: [String] }); …和另一个称为Vehicle的模式: var VehicleSchema = new Schema({ make: { type: Schema.Types.ObjectId, ref: 'Make' }, model: { type: Schema.Types.ObjectId, ref: 'Model' } }); 我需要一个填充车辆的查询,也需要车辆内的“制造”和“模型”字段。 我尝试过,但无法使其工作。 尝试与path也没有成功。 Chat.find(function (err, chats) { if (err) res.json({ success: false, response: err }); if (chats.length == […]

在linux上使用mongoose更新JSONtypes字段中的特定字段

我试图更新jsontypes字段内的特定值。 我的文档模式看起来像这样: character { name: {type: string}, experience: {type: json} … } 我确信经验总是包含一个带有2个字段的json对象: { unspent: number, total: number } 我试图做一个通用的更新方法,并在正常工作的Windows: 'update': function (itemid, update, callback) { model.update({'id': itemid}, update, {multi: false}, callback); } 更新得到一个req.body.fields对象,这是一个json对象,包含我想更新的'fields',例如 {'experience.total': 5} 这工作正常,当我在我的Windows开发框上运行它。 当我在我的linux登台服务器上尝试它时,mongoose似乎没有拿起它需要更新的东西。 我没有更新其他领域的问题(如我的例子中的“名称”) 谢谢!

mongodb聚合:平均和sorting

我是一个新手,并有一个MongoDB聚合的问题。 我用mongoose。 var SubjectScore = new Schema({ name: {type:String, required:true}, //math, science, history, … score: {type:Number, required:true } // 95, 85, 77,…. }); var Subject = new Schema({ year: Number, //2012, 2013, 2014 subjectScore : [SubjectScore] }); var StudentSchema = new Schema({ name: String, subject: [Subject], //array length varies for each student profile: String, }); […]

在node.js应用程序中引用自定义模式时出错

Node.js新手在这里…我有两个模式,我引用另一个。 var mongoose = require('mongoose'); var Schema = mongoose.Schema; /*Model for user settings */ var SettingsSchema = new Schema({ shoppingDay: {type: String, default : "Friday"}, shoppingFrequency : {type: String, default : "weekly"}, reminderEmails : {type: Boolean, default : true} }); module.exports = mongoose.model('Settings', SettingsSchema); 和 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Settings […]

Mongooseasynchronous调用另一个模型使validation不可能

我有两个mongoose模式,看起来像这样: var FlowsSchema = new Schema({ name: {type: String, required: true}, description: {type: String}, active: {type: Boolean, default: false}, product: {type: Schema.ObjectId, ref: 'ClientProduct'}, type: {type: Schema.ObjectId, ref: 'ConversionType'}, }); 然后将这个模式embedded到如下所示的父模式中: var ClientCampaignSchema = new Schema({ name: {type: String, required: true}, description: {type: String}, active: {type: Boolean, default: false}, activeFrom: {type: Date}, activeUntil: {type: Date}, […]