mongoose-schema

How can I get mongoose to push an array of urls to an array subdocument

喜夏-厌秋 提交于 2019-12-13 02:58:19
问题 I am making a blog system with multiple uploaded images, and multiple posts. I have created an upload screen which allows me to select a few of the previously images, and then posts that to the back end. This all works properly (thanks to some assistance I received at stack overflow), and the console gets this logged from the server: [ 'http://res.cloudinary.com/jacobsiler-com/image/upload/v1574344215/SilerGuitars/f8q5d4kedss1tpmhxmwg.jpg', 'http://res.cloudinary.com/jacobsiler-com/image

Making a MongoDB userSchema with specific privileges on each region

久未见 提交于 2019-12-13 02:49:03
问题 I have to make a page for entering some data about people on specific regions. Each user will have prvileges on a specific region. So I will make two collections into a DB. One for the users and one for the regions. The users will have access only to one region (or in some cases to more) and specific acceses, like read or write, or read&write, and so on. How can I model my schema so I can add more regions? That's how I did it: var mongoose = require("mongoose"), passportLocalMongoose =

Is it possible to query subdocuments directly using mongoose?

风格不统一 提交于 2019-12-12 23:00:41
问题 let's say there was a User model and a Post model. In this situation User's would have many posts; User would be the parent and Post would be the child. Is it possible to query for posts directly? For instance if I wanted to do something like app.get('/post/search/:query', (req,res) => { Posts.find({title: req.params.query }, (err,post) => { res.send(JSON.stringify(post)) }) }) or would one have to do: app.get('/post/search/:query',(req,res) => { let resultsFromQuery = []; User.find({'post

Mongoose - remove multiple documents in one function call

懵懂的女人 提交于 2019-12-12 10:28:42
问题 In documentation there's deleteMany() method Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {}); I want to remove multiple documents that have one common property and the other property vary. Something like this: Site.deleteMany({ userUID: uid, id: [10, 2, 3, 5]}, function(err) {} What would be the proper syntax for this? 回答1: I believe what youre looking for is the $in operator: Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err) {})

Mongoose - increment a field value in nested array

僤鯓⒐⒋嵵緔 提交于 2019-12-12 05:17:45
问题 I'm new to Mongoose and MongoDB and am trying to implement a scenario where I have to update a field value residing in a nested array. For example, my DB entry is as follows: { "_id" : ObjectId("581999ecf97860b77c1625f6"), // this is userID "username" : "test", "__v" : 0, "polls" : [ { "url" : "xxxx", "question" : "city or united", "_id" : ObjectId("581999ec3ba2b15ad7fbcd50"), //this is questionID "option" : [ { "votes" : 0, "value" : "city", "_id" : ObjectId("581999ec3ba2b15ad7fbcd52") /

Mongodb Schema for Posts and Shares

痴心易碎 提交于 2019-12-12 04:54:28
问题 I am new to mongodb NoSQL concept and stuck at point where I am unable to take a decision for modelling the schema that could best serve my purpose. I need to design schema in such a way that I have my end result as Posts and Shares sorted by time . For this I considered two options: Option 1: Different Collection for Posts and Share as: Schema for Post collection : var postSchema = mongoose.Schema({ postText: String, postedBy: String, privacy: Number, updatedOn: { type: Date, default: Date

How to use mongoose schema when you have dynamic values?

不羁岁月 提交于 2019-12-12 04:12:24
问题 I am trying to create schema where body can have different keys in it based on the incoming event. So when i try to rendered data it just send _id to client event is not part of results. Did i implemented wrong schema with for this approach ? event.model.js var mongoose = require('bluebird').promisifyAll(require('mongoose')); var bmpEventSchema = new mongoose.Schema({ event: { type: String, body : {} } }); export default mongoose.model('BmpEvent', bmpEventSchema); JsonDocument { "_id" :

Mongoose needs to create objects, but skip the property if another object already has this property

好久不见. 提交于 2019-12-12 03:28:12
问题 I know the title is confusing but let me explain more clearly. Here is what my mongoose schema looks like: var LocationSchema = new Schema({ locationKey: {type: String, unique: true}, wheat: Array, barley: Array, cty: {type: String, unique: true}, twp: {type: String, index: { unique: true} , dropDups: true}, rge: {type: String, unique: true}, }); I wrote some code that will create 3500 locations using this schema. The problem is that lots of locations have the same value for twp . When this

Nodejs, Mongoose, Express: type: Schema.ObjectId, ref: 'Account' not working

青春壹個敷衍的年華 提交于 2019-12-12 02:44:13
问题 I am just getting started in the MEAN stack and can't seem to get my object ID shared across multiple schemas. In a nutshell: I want the object Id from Account model to be stored as the Object Id in my Profile Model. I am using localpassport authentication. The following is my App.js file, routes, and models. Thanks in advance for any help. App.js var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var

Removing a child element in subdocument not working

99封情书 提交于 2019-12-12 01:27:51
问题 I'm fairly new to Mongoose and don't think my approach on deleting an item in a subdocument is the right one. I have the following schema setup: //DEPENDENCIES var mongoose = require('mongoose'); var contactSchema = new mongoose.Schema({ name:{type:String}, age:{type:Number} }); var phoneSchema = new mongoose.Schema({ number:{ type: String }, phoneType:{ type: Number } }) var memberSchema = new mongoose.Schema({ firstname: { type: String }, lastname: { type: String }, phone:[phoneSchema],