mongoose

Arrays of schema types on Mongoose

被刻印的时光 ゝ 提交于 2021-02-07 08:20:37
问题 I have a schema: var s = new Schema({ links: { type: [Url] } }); In this case I am using the url schema type from https://github.com/bnoguchi/mongoose-types - but I have tried this with other types. Mongoose doesn't seem to validate/use the schema type when in an array - works fine without the array. How can I define an array of schema types that will validate? 回答1: Answer from Mongoose creator: "Unless the Url is a subdocument, validation will not get triggered currently (there is a ticket

How to create Mongodb schema dynamically using nodejs

孤街浪徒 提交于 2021-02-07 07:23:40
问题 I'm wondering if it's possible to create a table dynamically in mongodb using a Mongoose schema, Node.js and Angular for example. The basic way to make a schema is to create a model explicitly in Node.js like this: import mongoose from 'mongoose'; const Schema = mongoose.Schema; const postSchema = new Schema({ title: { type: 'String', required: true }, content: { type: 'String', required: true }, slug: { type: 'String', required: true } }); let Post = mongoose.model('Post', postSchema); Is it

Using mongoose to add new field to existing document in mongodb through update method

删除回忆录丶 提交于 2021-02-07 05:27:31
问题 I'm trying to update a mongodb document with mongoose in node.js When the field exists, the update works, but if the field does not exist, the update will not work. I want to add new fields to the document. the MongoDB query ($set) is working: db.getCollection('users').update({ 'uid': 'uid' }, { $set: { 'vehicle_status': 'bike' } }) But it will not work when done in mongoose. Using $set does not add a field. User.update({ uid: uid }, { $set: { vehicle_status: vehicleSatus } }, { multi: true }

Using mongoose to add new field to existing document in mongodb through update method

余生长醉 提交于 2021-02-07 05:27:11
问题 I'm trying to update a mongodb document with mongoose in node.js When the field exists, the update works, but if the field does not exist, the update will not work. I want to add new fields to the document. the MongoDB query ($set) is working: db.getCollection('users').update({ 'uid': 'uid' }, { $set: { 'vehicle_status': 'bike' } }) But it will not work when done in mongoose. Using $set does not add a field. User.update({ uid: uid }, { $set: { vehicle_status: vehicleSatus } }, { multi: true }

What is the best practice to connect/disconnect to a database?

烈酒焚心 提交于 2021-02-06 12:48:56
问题 I'd like to know how to work with connectivity to a database in MEAN stack application. In particular, when should I create a connection to a database and when should I destroy a connection to a database. Should I create and destroy a connection on every new HTTP request or should I store a once created connection and use it for any subsequent requests as long as possible. I use Mongoose as a modeling tool. Here is an example. This is my routes.js file with a route /index . A request to this

What is the best practice to connect/disconnect to a database?

馋奶兔 提交于 2021-02-06 12:47:53
问题 I'd like to know how to work with connectivity to a database in MEAN stack application. In particular, when should I create a connection to a database and when should I destroy a connection to a database. Should I create and destroy a connection on every new HTTP request or should I store a once created connection and use it for any subsequent requests as long as possible. I use Mongoose as a modeling tool. Here is an example. This is my routes.js file with a route /index . A request to this

What is the best practice to connect/disconnect to a database?

╄→尐↘猪︶ㄣ 提交于 2021-02-06 12:47:09
问题 I'd like to know how to work with connectivity to a database in MEAN stack application. In particular, when should I create a connection to a database and when should I destroy a connection to a database. Should I create and destroy a connection on every new HTTP request or should I store a once created connection and use it for any subsequent requests as long as possible. I use Mongoose as a modeling tool. Here is an example. This is my routes.js file with a route /index . A request to this

Batch update with Mongoose

点点圈 提交于 2021-02-06 11:23:07
问题 I'm pulling data from a RETS(XML) feed and saving it in a local MongoDB using node and mongoose. Periodically I need to update the documents and delete the inactive ones as well as add new ones. Rather than making multiple queries to Mongo or the RETS server, I was pulling both and looping through the data. This works fine but is there a way to save the Mongoose results back to the database with updates and inserts? Or do I need to find each document and update it individually? 回答1: On

Batch update with Mongoose

我的未来我决定 提交于 2021-02-06 11:21:32
问题 I'm pulling data from a RETS(XML) feed and saving it in a local MongoDB using node and mongoose. Periodically I need to update the documents and delete the inactive ones as well as add new ones. Rather than making multiple queries to Mongo or the RETS server, I was pulling both and looping through the data. This works fine but is there a way to save the Mongoose results back to the database with updates and inserts? Or do I need to find each document and update it individually? 回答1: On

What is the correct way of using Bluebird for Mongoose promises?

风格不统一 提交于 2021-02-06 02:40:37
问题 I've been reading documentaion and articles and everyone seems to describe a different way about using Mongoose and Bluebird together. Even the official Mongoose documentation says something and Bluebird documentaion says another thing. According to Mongoose: mongoose.Promise = require('bluebird'); According to Bluebird: var Promise = require("bluebird"); Promise.promisifyAll(require("mongoose")); So to my understanding, if you pick the Mongoose way a sample query would be like: User.findById