mongoose-schema

Mongo schema, array of string with unique values

£可爱£侵袭症+ 提交于 2019-12-08 01:01:28
问题 I'm creating the schema for a mongo document and I can do everything except prevent duplicates in a non-object array. I'm aware of the addToSet, but I'm referring to Mongo Schema. I don't want to check on Update using $addToSet, rather I want this to be part of my schema validation. Example below. let sampleSchema = { name: { type: 'String', unique: true }, tags: [{ type: 'String', unique: true }] } The above snippet prevents name from having duplicate values. It allows tags to be stored as a

How to create mongoose schema with array of objects

丶灬走出姿态 提交于 2019-12-07 10:45:12
问题 I have this json: { "data": [ "id": "1", "name": "Sample test", "description": "this is a sample test", "category": "tests", "points": 100, "startDate"​:​"2018-02-15 00:00:00"​, "endDate"​:​"2018-02-22 00:00:00"​, "isActive"​:​true​, "alreadyAnswered"​:​false​, "questions"​:[ { "id": 1, "text": "What is your name", "type": "text", }, { "id": 2, "text": "What is your favorite color", "type": "select", "options": [ { "id": 1, "text": "Red", "value": "red" }, { "id": 2, "text": "Blue", "value":

nested id object not getting deleted in mongoose

笑着哭i 提交于 2019-12-07 02:57:28
Hello I am trying to delete pushed (data) _id from a document array, but I am getting no response while executing. Also, since it is a relational _id which I am trying to delete. How can I delete from the collection it is actually stored ? here is my delete route:- router.delete('/userDelete/:userId', checkAuth , (req, res, next) =>{ if(req.userData.role2 === 'admin') { Admin.findOneAndDelete({_id: req.params.userId},{ $pull: { 'admins.users': {_id: req.params._id}}},{new: true}) .exec() .then(result => { res.status(200).send(["Deleted"]); }) .catch(err =>{ if (err.code == 500) res.status(500)

Are there ways to populate a schema in Joi from another schema (like Mongoose does)?

安稳与你 提交于 2019-12-06 15:35:16
I'm currently only using Joi to validate and building schemas. However I feel like I'm missing features like reference another schema like Mongoose does. Or is the only way to do this by using both (or mongoose only)? I want to use something similar to this: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const personSchema = Schema({ _id: Schema.Types.ObjectId, name: String, age: Number, stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); const storySchema = Schema({ author: { type: Schema.Types.ObjectId, ref: 'Person' }, title: String, fans: [{ type: Schema

Pass a hardcoded value through mongoose schema

核能气质少年 提交于 2019-12-06 09:21:56
问题 I would like to post some hard coded values along with user input(variables) every time. args: [{ type: mongoose.Schema.Types.Mixed, required: true }] >>in this array i would like to pass some hard coded values along with user input variables. Well my data to be posted looks like this. {"file": "**<user input>**","name":"<user input>", "className": "com.izac.Parser.IO", "args": ["-i", "{\"enrichedKafkaOptions\": {\"checkpointLocation\": \"**<hard coded path always remains the same>**", \

Mongoose Schema with nested optional object

僤鯓⒐⒋嵵緔 提交于 2019-12-05 22:51:38
问题 Using the following schema: { data1: String, nested: { nestedProp1: String, nestedSub: [String] } } When I do new MyModel({data1: 'something}).toObject() shows the newly created document like this: { '_id' : 'xxxxx', 'data1': 'something', 'nested': { 'nestedSub': [] } } I.e. the nested document is created with the empty array. How do I make "nested" to be fully optional - i.e. not created at all if it is not provided on the input data? I do not want to use a separate schema for the "nested",

How to create mongoose schema with array of objects

核能气质少年 提交于 2019-12-05 14:26:06
I have this json: { "data": [ "id": "1", "name": "Sample test", "description": "this is a sample test", "category": "tests", "points": 100, "startDate"​:​"2018-02-15 00:00:00"​, "endDate"​:​"2018-02-22 00:00:00"​, "isActive"​:​true​, "alreadyAnswered"​:​false​, "questions"​:[ { "id": 1, "text": "What is your name", "type": "text", }, { "id": 2, "text": "What is your favorite color", "type": "select", "options": [ { "id": 1, "text": "Red", "value": "red" }, { "id": 2, "text": "Blue", "value": "blue" } ] } ] ] } I need to create this json into mongo database so I can get it via my node

How to restrict delete in MongoDB for relationship collection

旧巷老猫 提交于 2019-12-05 10:40:17
问题 Below models we can consider for example: Companies {Name: string, Address:string} Employees {FirstName: string, lastName:String, Company: {type:mongoose.schema.objectID,ref:companies} } Need to restrict deletion of companies which has employee records at database level, without using "pre" middleware. I am looking for some solution which is similar to MySQL relationships constraint - on delete restrict. 回答1: We can do this using $nin let us have Companies collection with 3 records db

Skip timestamps middleware for certain updates in Mongoose

淺唱寂寞╮ 提交于 2019-12-05 02:42:24
My application uses Mongoose and has a schema that uses the timestamps option : var fooSchema = new Schema({ name: String, }, { timestamps: true, }); mongoose.model('Foo', fooSchema); So whenever an update is written to the collection, the updatedAt property is changed to the current date. But now I would like to add some changes that should not update the updatedAt property. Some answers on Stackoverflow ( example ) suggested to use Foo.collection as that allegedly accesses the native MongoDB driver. So I tried this: Foo.collection.update({ _id: someFooId }, { $set: { name: 'New Name' } });

How to get a list of available Mongoose Discriminators?

限于喜欢 提交于 2019-12-05 02:29:10
Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically determine how many discriminations/inheritances/roles of the User model are available, as well as the available names? My question in terms of code: File : models/user.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var options = {discriminatorKey: 'role'}; var userSchema = mongoose.Schema({ name: String, email: String,