mongoose-schema

How to create a Mongoose schema from JSON

心不动则不痛 提交于 2019-12-05 01:05:50
问题 I am new into mongodb, nodejs and mongooseJS. Lately, I have been trying to create a mongoose schema for my JSON. { "endpoints":["a","z"], "poi":[{ "location_name": "a", "latitude": " 10.1075702", "longitude": "76.345662", "distance" : "0.0" }, { "location_name": "b", "latitude": "10.110199", "longitude": "76.3489361", "distance" : "2.0" }, { "location_name": "c", "latitude": "10.1197471", "longitude": "76.342873", "distance" : "3.1" }, { "location_name": "d", "latitude": "10.1254479",

Validate object against Mongoose schema without saving as a new document

北慕城南 提交于 2019-12-04 10:52:37
I'm trying to validate some data that will be inserted into a new document, but not before a lot of other things need to happen. So I was going to add a function to the static methods that would hopefully validate objects in an array against he model schema. Heres the code thus far: module.exports = Mongoose => { const Schema = Mongoose.Schema const peopleSchema = new Schema({ name: { type: Schema.Types.String, required: true, minlength: 3, maxlength: 25 }, age: Schema.Types.Number }) /** * Validate the settings of an array of people * * @param {array} people Array of people (objects) *

MongoDB E11000 duplicate key error

元气小坏坏 提交于 2019-12-04 06:03:36
I have a model that keeps erroring out after the first POST. I'm creating a scheduling application, which is X number of days, with rooms, and time slots for the rooms. The issue I'm having is creating Day Objects in the database. For sake of easy reading I'm just going to have a single key value pair day.model.js var mongoose = require('mongoose'); // Day Schema var daySchema = mongoose.Schema({ name:{ type: String, required: true, }, createdAt:{ type: Date, default: Date.now } }); var Day = module.exports = mongoose.model('Day', daySchema); // Get all Days module.exports.getDays = function

Easy way to increment Mongoose document versions for any update queries?

自闭症网瘾萝莉.ら 提交于 2019-12-04 04:10:52
I want to start taking advantage of Mongooses document versioning (__v key). I was having an issue actually incrementing the version value, then I found that you have to add this.increment() when executing a query. Is there a way to have automatically incremented? For now, I just added it to the pre middleware for a update-type queries: module.exports = Mongoose => { const Schema = Mongoose.Schema const modelSchema = new Schema( { name: Schema.Types.String, description: Schema.Types.String } ) // Any middleware that needs to be fired off for any/all update-type queries _.forEach( [ 'save',

Mongoose Promise error

こ雲淡風輕ζ 提交于 2019-12-04 03:36:09
问题 This is the error which is still being thrown when saving even after adding native promise. (node:5604) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html mongoose.Promise = global.Promise; mongoose.connect('mongodb://127.0.0.1/optimusCP') .then(function () { console.log('Connected to MONGOD !!'); }).catch(function (err) { console.log('Failed to establish connection with

mongoose TypeError: Schema is not a constructor

自闭症网瘾萝莉.ら 提交于 2019-12-03 10:45:49
I've encountered a strange thing. I have several mongoose models - and in one of them (only in one!) I get this error: TypeError: Schema is not a constructor I find it very strange as I have several working schemas. I tried logging mongoose.Schema in the non-working schema and it is indeed different from the mongoose.Schema in my working schemas - how is that possible? The code is almost identical. Here's the code for the non-working schema: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var errSchema = new Schema({ name: String, images:[{ type:String }], sizes:[{ type:

Mongoose document references with a one-to-many relationship

怎甘沉沦 提交于 2019-12-03 03:30:58
问题 I'm working on designing a database structure for a new project, and I'm pretty new to MongoDB, and obviously Mongoose. I've read Mongooses population documentation, where it has a one-to-many relationship, with one Person document to many Story documents, but the part that confuses me is where instead of the Story documents referencing what Person document it belongs to, the Person schema has it setup so it has an array of what Story documents it 'owns'. I'm setting up something very similar

MongoDB: Checking if nested array contains sub-array

前提是你 提交于 2019-12-02 12:33:29
I have a use case where the database is modelled like this: name: XYZ gradeCards: [{ id: 1234, // id of the report card comments: ['GOOD','NICE','WOW'] }, { id: 2345, comments: ['GOOD','NICE TRY'] }] Now, I have a query that I would like to query the schema as follows: I would be given a list of ids and values. For example: the given list is as follows: [{ id: 1234, comments: ['GOOD','NICE'] },{ id: 2345, comments: ['GOOD'] }] In short the ID should be matching and the comments should be a sub-array of the comments array for that id and also, all the conditions specified in the array should be

populate nested array of nested ref

流过昼夜 提交于 2019-12-02 10:05:59
The structure of the project is structure The main file is index.js that is located in the main directory. This one has 'use strict' var mongoose = require('mongoose'); var app = require('./app'); var port = process.env.port || 3000; mongoose.connect('mongodb://localhost:27017/changeProducts',(err,res) =>{ if(err){ throw err; }else{ console.log("Base de datos funcionando correctamente.."); app.listen(port, function(){ console.log('Servidor nodejs corriendo(app.listen)... '); }); } }); The version of mongoose is 5.0 The app.js is 'use strict' var express = require('express'); var bodyParser =

Mongoose findOneAndUpdate — updating an object inside an array of objects

自作多情 提交于 2019-12-01 10:34:16
Take a look at this Schema: let userSchema = new Schema({ email: { type: String, required: true }, password: { type: String, required: true }, books: [{ cover: String, title: String, link: String, requests: { requestingUser: String, bookOffered: String, beingRequested: false } }], ip: String }); What I am trying to do is use findOneAndUpdate to (based on given information) find the correct user, and then find the correct book (from the array of books by given title) And update the requests field (which is another object.) Is this possible to accomplish with only one findOneAndUpdate query ? So