Tag: mongoose

mongoose:填写一个歧视的子文件

我想填充子文档的字段,这是一个常见模式( Notificationable分为Message或FriendRequest )的区分元素。 这个问题与这个问题非常相似: mongoosejs:从两个不同的模式中填充一个objectId数组 ,这是两年前没有解决的。 由于mongoose进化了,鉴别者也是,我再次提出这个问题。 我到目前为止所尝试的: Notification.find({_id: 'whatever'}) .populate({ path: 'payload', match: {type: 'Message'}, populate: ['author', 'messageThread'] }) .populate({ path: 'payload', match: {type: 'FriendRequest'}, populate: ['to', 'from'] }) .exec(); 这是行不通的,因为path是一样的。 所以我试了一下: Notification.find({_id: 'whatever'}) .populate({ path: 'payload', populate: [ { path: 'messageThread', match: {type: 'Message'}, }, { path: 'author', match: {type: 'Message'}, }, { […]

如何呈现两个数组ejs机智mongoose

你好每一个我是新的node.js我正在玩的代码通过build立一个小的论坛,用户可以写一些职位。 当我打开主页我希望有他的主人的每一个职位,但我得到的所有职位与同一用户和用户更改每次我刷新页面 这是我的模型用户 var mongoose=require('mongoose'), var Schema=mongoose.Schema; var UserSchema=new Schema({ username:String, email:String, password:String, insdate:{type:Date,default:Date.now}, isDeleted:{type:Boolean,default:false}, }); var User=mongoose.model("users",UserSchema); module.exports=User; 这是我的模型发布 var mongoose=require('mongoose'); var Schema=mongoose.Schema; var PostSchema=new Schema({ title: String, body: String, isDeleted:{type:Boolean,default:false}, added:{type:Date,default:Date.now}, owner:{ type:mongoose.Schema.Types.ObjectId, ref:'users' } }); var Post=mongoose.model("posts",PostSchema); module.exports=Post; 在这里我尝试发送数据到浏览器我使用ejs app.get('/', function (req, res) { //i want to fetch Post and User and […]

mongoose – 保存用户_id到不同的模式更改ID

我是新来的Mongo DB / Mongoose,并尝试将用户的ID添加到不同的架构时遇到问题。 首先这里是我的用户架构,按预期工作: const userSchema = new Schema({ email:{ type:String, unique:true, lowercase:true, trim:true, validate:[ validator.isEmail,'Invalid email address' ], required:'Please supply an email address' }, name:{ type:String, trim:true, required:'Please supply a name' }, userType:{ type:String, required:'Please supply a user type' }, teams:{ type:Array } }); userSchema.plugin(passportLocalMongoose,{usernameField:'email'}); userSchema.plugin(mongodbErrorHandler); module.exports = mongoose.model('User',userSchema) 其次,这里是我的团队架构,其中从用户模式创build的用户获取的_id出于某种原因存储为稍微不同的值: const teamSchema = […]

Mongo的数据库$推操作员不工作

我想通过使用req.params方法在mongo模型的对象中的数组中推送数据,但它给我错误: MongoError:不能使用部分(friends.username的朋友)遍历元素({friends:[]}) 这是我的代码: 后端 router.post('/accept/:id/:username',isLoggedIn,function(req,res){ User.findOneAndUpdate({username:req.user.username,'friendRequest._id':req.params.id}, {$set:{'friendRequest.$.Status':'Friend','friendRequest.$.Color':'green'}},function(err,founduser){ if (err) { throw err; } else { User.findOneAndUpdate({username:req.user.username}, {$push:{'friends.username':req.params.username}} ,function(err,founduser){ if (err) { throw err; } else { //Error is coming from this line User.findOneAndUpdate({username:req.params.username}, {$push:{'friends.username':req.user.username}} ,function(err,founduser){ if (err) { throw err; } else { res.render("back"); } }) } }) } }) }) **My Model** var mongoose […]

如何设置useMongoClient(Mongoose 4.11.0)?

这是我用来连接到我的数据库的代码: private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> { return Mongoose.connect(databaseUri).then(() => { debug('Connected to MongoDB at %O', databaseUri); return Mongoose.connection; }); } 今天我更新了Mongoose到版本4.11.0,并且在运行我的testing时遇到了这个警告: (node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()` 我找不到任何有关如何设置useMongoClient的信息。 你们知道如何?

用于创build基于组的应用程序访问的MongoDB参考

我想build立一个用户访问应用程序的function,如下所示: •创build用户•将用户保留在一个组中•将组分配给特定的应用程序•属于某个组的用户可以访问分配给他们的应用程序。 现在我很困惑创build用户和组架构。 var GroupSchema = new Schema({ groupTitle: { type: String, required: true }, users : [ { type: Schema.Types.ObjectId, ref: 'User', index: true } ] }); var Group = mongoose.model('Group', GroupSchema); module.exports = Group; 和用户架构是 var UserSchema = new Schema({ username: { type: String, unique: true, required: true }, password: { type: String, […]

邮政数据后,mongoose复制条目

嘿:]尝试通过api.post('/send/remember-me', function (req, res)发送数据之后api.post('/send/remember-me', function (req, res)这些条目将在我的集合中重复出现,请参阅下面的内容 rememberMe 1 false 8.03 KB remembermes 1 false 16.08 KB 我正在这样做我的模式: Remember.js var mongoose = require('mongoose'); var rememberMeModel = mongoose.Schema({ username: { type: String } }); var rememberMe = module.exports = mongoose.model('rememberMe', rememberMeModel); index.js var rememberMe = require('./models/Remember'); api.post('/send/remember-me', function (req, res) { var rememberCredential = new rememberMe(); […]

Mongooses – 如何在过去两周内获得数据变化

假设我有一个书架。 它有几个领域。 const Book = new schema({ title: String, content: String, like: Number }); 如何得到过去2周内最喜欢的书? 最重要的是,它需要每天更新。 举个例子,假设我现在只需要过去两天的喜欢。 第一天共有5人喜欢这本书,第二天有7人喜欢这本书,第三天有3人。 因此,我预计第二天有12个喜欢(5 + 7) , 第三天有7个喜欢(7 + 3) 我打算添加一个字段和一个14元素的数组。 { …, likeCnt: { type: Array, default: Array(14).fill(0) } } 所以只有当有人喜欢这本书时才更新Date().getDate() % 14 th元素。 但是,我需要使用cron-job来每天将每本书的likeCnt[] 。 请告诉我一个更有效的解决scheme。 非常感谢。

Mongoose / MongoDB:从名称中获取_ids

给定一个string数组,并在MongoDB中使用该string和主键的集合,如果存在,如何获得这些string的_id,如果不存在,则插入logging? 该集合只有名称和它的_id。

如何将多个mongoose查询合并为一个

User.find().then(function (user) { console.log(user) }) Category.find().then(function (category) { console.log(err); }) Content.find().then(function (content) { console.log(content); }) 如何将查询语句组合成一个并获得所有结果? ps:我用mongoose来操作mongoDb。