Tag: mongodb

mongoose引用不起作用

我创build了两个Schema,User和Whisp(一个whisp就像一个tweet)。 //User var UserSchema = new mongoose.Schema({ username: { type: String, required: true, index: { unique: true } }, password: { type: String, required: true } }); UserModel = mongoose.model("UserSchema", UserSchema); module.exports.User = UserModel; //Whisp var WhispSchema = new mongoose.Schema({ text : String, created_at : Date, created_by : {type : Schema.Types.ObjectId, ref : "UserSchema"}//i want […]

使用Mongoose引用具有未知模型types的对象

使用Mongoose时 ,有可能有一个字段引用另一个对象,当该文档的模型/types是未知的? 例如,我有模型:照片,评论,提交,post等,我想有一个Like模型,指向他们: var Like = new Mongoose.Schema({ // What would the value of `ref` be, should it just be left out? target: { type: Schema.Types.ObjectId, ref: '*' } }); 根据我的理解, ref需要成为一个模型。 我可以把它全部放在一起,但是我仍然可以从这种方式得到Mongoose 填充方法的好处吗?

使用NodeJS和jQuery查询MongDB数据库

我试图查询使用MongoDB(Mongoose作为ODM)构build的数据库并在页面上显示结果。 数据库将数据(包括date:'date'和链接:'wwww.example.com')存储在名为'entries'的集合中。 我有两个想用来访问数据库的视图:一个视图,它将利用date键在特定的日子显示正确的链接; 以及我可以查看,添加和删除条目的数据库视图。 我不知何故能够遍历数据库视图的条目,并将它们添加到DOM,但是当我尝试相同的部件视图,我似乎无法从条目集合访问​​JSON对象。 我使用Jade作为我的HTML的预处理器 我的app.js文件是这样的: var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var router = express.Router(); //database stuff var mongoose = require('mongoose'); var Schema = mongoose.Schema; //connect to the data store and the set up the […]

Find()函数retunrs undefined – Mongoose和Node.js

我试图用Node.js和Mongoose做一个简单的函数,如果模型是空的,返回true。 mongooseconfiguration是好的: var mongoose = require('mongoose'); var db = mongoose.createConnection( 'mongodb://localhost:27017/prueba' ); var userSchema = mongoose.Schema({ phoneNumber: Number, name: String }); var User = db.model('User', userSchema, 'User''); 然后我试图做到这一点: User.find(function(err, data) { if (err) {console.log(err)}; console.log(data.length == 0 ); }); 它工作正常,它logging真实,或错误。 然后我试着做: var isUsersEmpty = function () { User.find(function(err, data) { if (err) {console.log(err)}; console.log(data.length == […]

用自定义的UID设置mongo的_id

背景: 我无法控制来自我的数据库文档“ 事情”的事件 。 当post进来时,我写了一个名为findOrCreate()的函数(它会在Things文档中find或创build一个logging(object?))。 为了减less冗余,我想使用第三方POST负载字段nuid来replace该logging(对象)的默认_id 。 题: 我如何正确地replace_id与有效载荷字段nuid ? 示例负载: curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/things 架构 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var ThingSchema = new Schema( {nuid: String} ); module.exports = mongoose.model('Thing', ThingSchema); thing.controller.js //*currently this function will have both _id and a nuid (which works […]

mongoosesorting填充字段

以及我有以下shemas: var BrandSchema = new Schema({ 名称: { 键入:String, 要求:真, 索引:{ 独特:真实 }, 小写:真 }, 商标: { 键入:ObjectId, ref:'Image' } }); var FollowActionSchema = new Schema({ 'actionDate':{ 键入:date, 'default':Date.now }, '牌': { 键入:ObjectId, ref:'品牌' }, “表演者”:{ 键入:ObjectId, ref:'User' }, 'type':string//跟随用户,跟随品牌,跟随买家 }); 我想要的是让用户跟随品牌,按品牌名称sorting,所以为了做到这一点,我做了FollowAction的查询,并find用户所做的所有FollowActions,然后填充品牌字段。 所以问题是我不能sorting查询的品牌名称,唯一的方法,我知道这样做是通过返回所有文件,并从nodejs应用程序sorting。 任何人都知道我该怎么做? 或者如果我应该改变shema结构? 我做的查询是: async.waterfall([ 函数findActions(next){ var options = { 查询:{ 执行者:userId, $或:[{ […]

PencilBlue – 自定义对象ID

我正在使用PencilBlue,而且我在自定义对象Id中遇到了麻烦。 “名称”字段用作Id,但我希望它只是一个常规字段,并有一个“ID”字段作为标识符。 我怎么能做到这一点?

从mongo获取不同的坐标

这是我的Mongo模型: Schema = new mongoose.Schema({ userId: {type: mongoose.Schema.Types.ObjectId, required: true, index: true, ref: 'user'}, date: {type: Number, index: true}, coords: {type: [Number], index: '2dsphere', required: true} //not distinct } 我试图按以下格式按date检索不同的坐标: [[long, lat],[long, lat],[long, lat], …, [long, lat]] 我写了这样的东西: module.exports = function(req, res, next) { var days = req.params.days || 365; db.model('activity').aggregate([ {$match: {date: {$gte: getDatesFromNumDays(days)[0], […]

在单独的ec2实例上连接到mongodb

我在AWS上运行两个不同的实例,一个用于节点应用程序,另一个用于mongoDB。 我试图连接到其他实例上的mongoDB,但无法和“504网关超时”失败。 我的db_conf.js连接到节点应用程序如下所示: var express = require('express'); var mongodb = require('mongodb'); var url = "mongodb://<PUBLIC IP of mongoDB instance>:27017/local"; module.exports = url; 我在mongodb.conf中注释了“bind_ip”并重新启动了mongoDB。 另外,我已经从mongoDB实例的安全组中打开了节点应用程序服务器公共IP的27017端口,但是没有使用。 请提出一个方法来实现这个(如果有的话)。 提前致谢 :)

你如何find使用MongoDB节点驱动程序的ID?

我正在寻找使用MongoDB Node驱动程序通过id查找的最佳方法。 我看到, _id字段是一个ObjectId ,所以使用该string的id将无法正常工作。 我看到你可以使用ObjectId ,如下所示: var ObjectId = require('mongodb').ObjectId; exports.show = function(req, res) { var db = req.app.get('db'); var id = new ObjectId(req.params.id); db.collection('posts').findOne({ _id: id }, function(err, post) { res.status(200).json(post); }); }; 但是有没有最好的做法/更方便的方法? 我没有遇到过查看文档的人。 注意:Mongoose有一个findById方法,你可以使用idstring。