Tag: mongoose

Mongoose / MongoDB中嵌套模式的正确模式是什么?

对我来说,做下面的事情似乎是合乎逻辑的: var AvatarSchema = new Mongoose.Schema({ type: String, url: String }); var UserSchema = new Mongoose.Schema({ avatars: [AvatarSchema], email: String, name: String, salt: String, token: String }); var ThinUserSchema = new Mongoose.Schema({ avatars: [AvatarSchema], email: String, firstname: String, lastname: String, }); var QuestionSchema = new Mongoose.Schema({ question: String, users: [ThinUserSchema] }); 然后再。 。 。像下面这样的东西: var […]

使用Mongoose,Express和AngularJS上传图片

我知道这个问题以前已经被问过很多次了,而且我几乎可以读到关于这个问题的所有东西,比如: https://stackoverflow.com/a/25022437/1031184 使用Node.js,Express和Mongoose上传图像 这些是迄今为止我发现的最好的。 我的问题是,他们还不是很清楚,关于这方面的文档很less,而且讨论似乎针对那些比我更先进的人。 所以,如果有人可以请我走,但如何使用Mongoose,Express&AngularJS上传图片,我真的会喜欢它。 我实际上正在使用平均满量程。 (这个生成器是精确的 – https://github.com/DaftMonk/generator-angular-fullstack ) AddController: 'use strict'; angular.module('lumicaApp') .controller('ProjectAddCtrl', ['$scope', '$location', '$log', 'projectsModel', 'users', 'types', function ($scope, $location, $log, projectsModel, users, types) { $scope.dismiss = function () { $scope.$dismiss(); }; $scope.users = users; $scope.types = types; $scope.project = { name: null, type: null, images: { thumbnail: null // […]

mongoose与模式密钥唯一重复

我想在整个集合中使关键项目独一无二,但是我无法得到这个工作,我在这里发现了类似的问题。 task.js function make(Schema, mongoose) { var Tasks = new Schema({ project: { type: String, index: { unique: true, dropDups: true }}, description: String }); mongoose.model('Task', Tasks); } module.exports.make = make; test.js var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/rss'); var Schema = mongoose.Schema , ObjectId = Schema.ObjectId; require('./task.js').make(Schema, mongoose); var Task = mongoose.model('Task'); var newTask = new […]

dynamic创build收集瓦特/mongoose

我想让用户能够在我的Node应用程序中创build集合。 我真的只看到mongoose藏品中硬编码的例子。 任何人都知道是否有可能dynamic创build集合w / mongoose? 如果是这样,一个例子会非常有帮助。 基本上我想能够在不同的集合中存储不同“事件”的数据。 IE事件:event1,event2,… eventN 用户可以创build自己的自定义事件并在该集合中存储数据。 最后,每个事件可能有数百/数千行。 我想让用户能够对他们的事件执行CRUD操作。 我不希望将每个事件数据存储在一个大集合中,而是将其存储在不同的集合中。 我没有真正的例子,因为我只用mongoose创build了“硬编码”的集合。 我甚至不确定我可以根据用户请求在mongoose中创build一个新的集合。 var mongoose = require('mongoose'); mongoose.connect('localhost', 'events'); var schema = mongoose.Schema({ name: 'string' }); var Event1 = mongoose.model('Event1', schema); var event1= new Event1({ name: 'something' }); event1.save(function (err) { if (err) // … console.log('meow'); }); 以上工作很好,如果我硬编码'Event1'作为一个集合。 不知道我创build了一个dynamic集合。 var mongoose = require('mongoose'); […]

Node.js – Mongoose – 检查一个集合是否存在

我需要使用mongoose插入一些数据,但集合的名称是由用户在插入时提供的,所以我首先必须检查集合是否存在。 我知道如何检查一个集合是否存在的方式是通过查询system.namespaces集合。 我可以看到3种可能的方法来做到这一点。 find一种方法来使用mongoose查询system.namespaces (也许定义一个模式匹配的数据库)。 从mongoose中获取一些底层的node-mongodb-native对象并手动执行查询。 无论如何,这是我想学习如何做的事情。 使用一个独立的node-mongodb-native(或其他驱动程序)实例来执行查询 数字3是最不优雅,我试图避免的,我不想加载驱动程序的另一个实例,也没有创build一个新的连接时,mongoose已经创build一个。 写这个之后我要试试1号。 我刚刚检查了system.namespaces ,模式看起来很简单 我还想听听一些意见。 谢谢!

在Mongoose中find一个子文档

我正在尝试findOne查询在Mongoose上的一个子文档,但我没有太多的运气… 我的模式看起来像这样: var Team = mongoose.Schema({ teamName: String, teamURL: String, teamMembers: [{username: String, password: String, email: String, dateCreated: Date}], }); var Team = db.model('Team', Team); 我只需要从我使用此查询的文档中find用户的电子邮件 Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1}, function (err, team) { if (team) { console.log(team[1].email); } }); 任何帮助,将不胜感激!

mongoose – 检查ObjectId是否存在于一个数组中

以下是一个示例模型: UserModel == { name: String, friends: [ObjectId], } friends对应于一些其他模型的对象的id列表,例如AboutModel 。 AboutModel == { name: String, } User.findOne({name: 'Alpha'}, function(error, user){ About.find({}, function(error, abouts){ // consider abouts are all unique in this case var doStuff = function(index){ if (!(about.id in user.friends)){ user.friends.push(about.id); about.save(); } if (index + 1 < abouts.length){ doStuff(index + 1) } } […]

我应该如何将价格存入mongoose?

我使用mongoose模式与node.js一起使用了express-validator(它具有节点validation器santiziations和validation器)。 什么是存储物品价格的好方法? 我现在有 var ItemSchema = new Schema({ name : { type: String, required: true, trim: true } , price : Number }); 价格是可选的,所以我有: if ( req.body.price ) { req.sanitize('price').toFloat(); req.assert('price', 'Enter a price (number only)').isFloat(); } expression式validation器给我isNumeric(允许0填充),isDecimal,isInt …我宁愿只是转换为十进制和剥离所有字符,所以我总是插入42.00分贝。 我想让他们input42.00美元,42美元,42美元,42.00美元,只是存储42.00。 我怎样才能做到这一点? 并仍然validation我看到类似于数字的东西,例如,如果他们input'abc'我想要使用req.assert将错误返回到表单。 另外,我想货币最终会成为一个问题… 更新,我发现这个职位,说存储价格以美分整数,所以4200 https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use 当我打电话给item.price时,我只需要一种将4200转换为$ 42.00的方法,并将input清理并转换为4200。

Mongoose在数组中删除(拉)一个文档,不能和ObjectID一起工作

我有以下mongoose模式: user = { "userId" : "myId", "connections": [{ "dateConnectedUnix": 1334567891, "isActive": true }, { "dateConnectedUnix": 1334567893, "isActive": false }] } 我想删除connections数组中的第二个项目,以获得以下内容: user = { "userId" : "myId", "connections": [{ "dateConnectedUnix": 1334567893, "isActive": false }] } 下面的代码按预期工作: userAccounts.update({'connections.isActive': false }, {$pull: { 'connections.isActive':false }}, function (err,val) { console.log(val) }); 但是,我需要删除基于ObjectId。 以下是不行的: userAccounts.update({'connections._id': '1234-someId-6789' }, {$pull: […]

用Mongoose,Express,NodeJS更新模型

我试图更新一个实例化的模型('地方' – 我知道它从其他路线工作)在MongoDB中,花了一段时间,试图正确这样做。 我也试着重新回到查看“地点”来查看更新属性的页面。 Node v0.4.0,Express v1.0.7,Mongoose 1.10.0 架构: var PlaceSchema = new Schema({ name :String , capital: String , continent: String }); 控制器/路线: app.put('/places/:name', function(req, res) { var name = req.body.name; var capital = req.body.capital; var continent = req.body.continent; Place.update({ name: name, capital: capital, continent: continent}, function(name) { res.redirect('/places/'+name) }); }); 我尝试了一堆不同的方式,但似乎无法得到它。 另外,是不是我如何宣布阻止进一步操作的三个(名称,资本和大陆)variables? 谢谢。 […]