mongoose-schema

Mongoose - Generate ObjectID for each object before saving document

丶灬走出姿态 提交于 2019-12-01 08:37:24
问题 I want generate an ObjectID for each Object present inside my array. The thing is I'm getting the products with a .forEach statement from another server and push them inside my array without a Schema that generates an ObjectID.... Product Schema: const productsSchema = new mongoose.Schema({ apiKey: String, domain: String, totalcount: Number, totaldone: Number, allSKUS: Array, allProducts: Array, created_at: { type: Date }, updated_at: { type: Date }, }, { collection: 'products', timestamps:

Mongoose advanced custom schema object type

亡梦爱人 提交于 2019-11-30 15:17:11
问题 I couldn't find any example of an advanced custom schema type involving custom objects (or value-objects) in Mongoose >=4.4. Imagine that I want to use a custom type like: function Polygon(c) { this.bounds = [ /* some data */ ]; this.npoints = /* ... */ /* ... initialize polygon ... */ }; Polygon.prototype.area = function surfaceArea() { /**/ }; Polygon.prototype.toObject = function toObject() { return this.bounds; }; Next, I implement a custom SchemaType like: function PolygonType(key,

Nested objects in mongoose schemas

泪湿孤枕 提交于 2019-11-30 00:16:09
i've seen many answers to this question here, but i still don't get it (maybe because they use more "complex" examples)... So what im trying to do is a schema for a "Customer", and it will have two fields that will have nested "subfields", and others that may repeat. here is what i mean: let customerModel = new Schema({ firstName: String, lastName: String, company: String, contactInfo: { tel: [Number], email: [String], address: { city: String, street: String, houseNumber: String } } }); tel and email might be an array. and address will not be repeated, but have some sub fields as you can see.

How to drop index using Mongoose

风流意气都作罢 提交于 2019-11-29 12:32:08
Mongoose Imagdata.dropIndexes( { "image_id": 1 }, function(err){ if(err){ res.send("error"); } else{ res.send("success"); } }); This is the code I used for remove the index of a field 'image_id'. When I trying to execute this function It shows "TypeError: Imagdata.dropIndexes is not a function". How to fix this issue... You need to use the raw collection property of your model to access the underlying MongoDB API: Imagdata.collection.dropIndex(...); 来源: https://stackoverflow.com/questions/34450547/how-to-drop-index-using-mongoose

Adding child document to existing mongodb document

笑着哭i 提交于 2019-11-29 12:05:09
问题 Essentially I am just trying to add a new sub-document to my existing mongodb document that has the following schema /models/server/destination.js // this is the "destination" model for mongoose var mongoose = require('mongoose') var Adventure = require('../models/adventure') // this is the schema that every entry will get when a new trip is made. var tripSchema = mongoose.Schema({ name: { type: String, required: true }, city: { type: String, required: true }, dateStart: { type: Date,

Find one or create with Mongoose

江枫思渺然 提交于 2019-11-28 10:58:06
I have Page.findById(pageId).then(page => { const pageId = page.id; .. }); My problem is that if no page id is given, it should just take the first available page given some conditions, which is done by Page.findOne({}).then(page => { const pageId = page.id; .. }); but if no page is found, it should create a new page and use this, which is done with Page.create({}).then(page => { const pageId = page.id; .. }); But how do I combine all this to as few lines as possible? I have a lot of logic going on inside page => { ... } so I would very much like to do this smart, so I can avoid doing it like

How to use mongoose model schema with dynamic keys?

China☆狼群 提交于 2019-11-28 05:00:16
问题 i'm using mongoose with nodejs, and i need to create a dynamic schema model, this is my code: schema.add({key : String}); key = "user_name", but in my db i found that the model take it as key { key : "Michele" } and not { user_name: "Michele"} What can i do? thank you. 回答1: The same issue schema with variable key is talked in mongoose, Nope not currently possible. Closest alternative is to use strict: false or the mixed schema type. 回答2: If I understand correctly, you want to add a new column