mongoose-schema

Can't add object to MongoDB array inside a document

风格不统一 提交于 2020-01-05 04:11:12
问题 I'm building an API for my chat application and I wrote the endpoint below to save new messages in MongoDB. The messages themselves are an array. Testing this endpoint with Postman returns the newly created message in the response but the message is not added to my array of messages. router.post('/:id/messages', async (request, response) => { const chatMessage = new Message({ type: request.body.type, body: request.body.body, author: request.body.author }); try { const newMessage = await

Mongoose and array of ref UUID's does not convert

两盒软妹~` 提交于 2020-01-04 06:22:20
问题 When using the library mongoose-uuid, I am able to setup UUID types for my schemas, so when I read the data it is in string (utf-8) format and when I save the data it is in UUID ObjectID BSON type 4 format. This works great with top level or flat direct values and ref definitions in my schema. However, when I have a UUID's in an array of ref's in a schema, the array saves to the database correctly, However when it is presented it is in its raw type. Based on the example below you can see

nested id object not getting deleted in mongoose

强颜欢笑 提交于 2020-01-03 01:47:12
问题 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()

import mongoose schema into another schema file makes the imported schema undefined

纵然是瞬间 提交于 2020-01-02 23:23:46
问题 File structure: │ resolvers.js │ schema.js │ └───schemas matchesSchema.js playersSchema.js teamsSchema.js tournamentsSchema.js So I have 4 schema's and I want to use the other schema's in the all my schema's but when I import it I get an error: C:\Users\phara0h\Dropbox\esports-scores.com\nodeTest\node_modules\mongoose\lib\schema.js:425 throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`'); ^ TypeError: Invalid value for schema Array path `matches` at Schema.add (C:

Should I Use Schema.Types.ObjectId or Schema.ObjectId When Defining a Mongoose Schema

杀马特。学长 韩版系。学妹 提交于 2020-01-01 04:18:11
问题 It seems like defining my Schema this way: var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.Types.ObjectId, ref:"Thing"} }); or this way: var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.ObjectId, ref:"Thing"} }); Both work. I see that the mongoose guide uses Schema.Types.ObjectId http://mongoosejs.com/docs/schematypes.html But I'm confused that both work. Which one should be used for the Schema? And what is the difference between the two?

Mongoose findOneAndUpdate — updating an object inside an array of objects

孤人 提交于 2019-12-30 11:26:48
问题 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

Nested objects in mongoose schemas

青春壹個敷衍的年華 提交于 2019-12-29 17:36:12
问题 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

mongodb filter documents with longitude,latitude and with particular distance

烂漫一生 提交于 2019-12-25 09:04:19
问题 My schema design: var LocationSchema = new Schema({ area: String, loc: [ type: [Number], // [<longitude>, <latitude>] index: '2d' // create the geospatial index ] }); module.exports = mongoose.model('Location', LocationSchema); Data has stored like this mongodb { "_id": { "$oid": "58c2796d734d1d46588666c6" }, "area": "madiwla", "loc": [ 12.9226, 77.6174 ] } { "_id": { "$oid": "58c27989734d1d4658866705" }, "area": "majestic", "loc": [ 12.9767, 77.5713 ] } { "_id": { "$oid":

how to insert particular array value in mongodb using angularjs

冷暖自知 提交于 2019-12-25 09:02:06
问题 I using angluarjs with mongodb. I have schema is called array based schema . schema structure : var Schema = new Schema({ UnitId: String, UnitName: String , Details1:[{ Details1ID:String, Name:String, Amount:Number }], Details2:[{ Details2ID:String, Name:String, Amount:Number }], Details3:[{ Details3ID:String, }], I want query for inserting the req.body for only Details3 . how to do this? NOTE:Req.body is Dynamic value 回答1: first, you need to add { strict : false } options to your schema var

populate embed array mongoose 5.0. Nested ref

旧城冷巷雨未停 提交于 2019-12-25 04:00:31
问题 This is the first model. It's located in the folder called models/user.js 'use strict' var mongoose = require('mongoose'); var Schema = mongoose.Schema; var UserSchema = Schema({ publications: [{ description: String, categories: [{ type: Schema.Types.ObjectId, ref: 'Category.subcategories' }] }] The model category. It's located in the folder called models/category.js 'use strict' var mongoose = require('mongoose'); var Schema = mongoose.Schema; var CategorySchema = Schema({ name: String,