mongoose

Mongoose: findOneAndUpdate does not update an existing field

送分小仙女□ 提交于 2021-01-29 19:01:24
问题 I am trying to fetch every minute a web API data to MongoDB, save it and update when there is any change. The problem is the volume (every field) field is updated only (happens after a new trading day starts) when the new date is added to ChildSchemaData - subdocumet. All this data is used to build a daily (NOT hourly/minute ) stock chart on front-end. And I am trying to stream it from database but since the volume (and all other fields in subdocument) is not updated in database nothing is

pre save middleware in mongoose

左心房为你撑大大i 提交于 2021-01-29 16:20:31
问题 i am first time using pre save middleware and getting a bit confusion in it. It runs perfectly fine and also my save method is getting executed eventhough i am not calling the next() case 1 tourSchema.pre('save', function () { console.log('first middleware is getting called'); }) But when i do like this when next is declared inside the function params but i don't call the next() it hangs there and the save method is not getting executed case 2 tourSchema.pre('save', function (next) { console

How to output a value from array Mongoose?

痞子三分冷 提交于 2021-01-29 15:37:05
问题 I have an array that contains the values: id, uid, level, I need to output the user id with level = 3 how do I do this? Sample code: "users": [{"id": 124, "uid": 2, "level": 1}, {"id": 553, "uid": 19, "level": 3}] 回答1: Using mongoose you can do this using findOne : yourModel.findOne({level:3},{id:1}).then(result => { console.log("result = ",result.id) }).catch(e => { // error }) Response is 553 Example how mongo query works here. This example output an array but ising findOne only one value

How to run mongo shell commands in Node.js Mongoose?

╄→гoц情女王★ 提交于 2021-01-29 15:06:32
问题 I would like to run the isMaster() command within my node.js project. The problem is that I'm unsure how to run any sort of mongo shell command via js code. I know in Python you can use client.admin.command('ismaster') . If this isn't achievable within mongoose, I am open to using the mongodb package, but I'd like to keep it solely mongoose. I am just trying to test if a connection is within a primary node. Thanks! 回答1: You can use either Db.executeDbAdminCommand or Db.admin().command. This

How to update field of object in array with Mongoose

孤街浪徒 提交于 2021-01-29 14:50:36
问题 I need to find a room by id and update received field to true where user id equals to xx . The document is here: { "_id" : ObjectId("5e5d0d870fc69641a41a3c65"), "users" : [ ObjectId("5e57d64d92cc878760086980"), ObjectId("5e57d64592cc87876008697e") ], "messages" : [ { "_id" : ObjectId("5e67834b6c8b2d356a4ad9fd"), "text" : "Hello", "user" : ObjectId("5e57d64d92cc878760086980"), "createdAt" : ISODate("2020-03-10T12:08:43.006Z"), "sent" : true, "received" : false }, { "_id" : ObjectId(

Node js form submit TypeError: Cannot set property 'userId' of undefined

淺唱寂寞╮ 提交于 2021-01-29 13:09:46
问题 I have added login and registration features to a page in my application in views/employee/login.hbs. When I enter in registration details (email, username, password and conf password), I hit the register button to hopefully be redirected to the views/employee/home.hbs page. However, when I complete this action my MongoDB Compass Community database receives the registration details that I have entered successfully but my browser states 'The site cannot be reached' and URL is "localhost:3000

How to add item to mongoose Array

南笙酒味 提交于 2021-01-29 11:00:23
问题 I'm developing a web app that uses mongodb database using mongoose in node.js... Now, I'm trying to build the rate feature, in this feature, people can rate the store and give some comments about that store. This is the structure: rate: { author: req.body.author, text: req.body.text } To update it I'm using the "findOneAndUpdate" function, but, Always when i do it, the existent rate is overwritten by the new... Can you guys help me? 回答1: Here you can do. I am just demonstrating with example

Solving relationships in Mongoose and GraphQL

↘锁芯ラ 提交于 2021-01-29 09:55:47
问题 So, I have this app that I'm starting and I'm going with MERNG stack using Apollo. I was doing research by reading blog posts and watching videos in order to have a better understanding of how GraphQL works (never actually worked with it). At some point, I started to see examples of relationships between db models such as "posts from a user". For example: import { model, Schema } from 'mongoose'; const postSchema = new Schema( { title: String, createdBy { type: Schema.Types.ObjectId, ref:

mongoose find and update removes the other fields

99封情书 提交于 2021-01-29 09:45:36
问题 I have schema like this: this.schema = new Schema({ userEmail: String environments: [ { envId: String, appPreference: String, language: String, timeZone: String, summaryNotificationSchedule: { timeOfTheDay: String } } ] }); Update request: { "envId": "u2", "appPreference": "put2", "timeZone": "gmt", "summaryNotificationSchedule.timeOfTheDay": "32400", } As you can see, I am not sending "language": "abc", in the update request and in the result I see the language field is removed. I want to

MongoDB: how to update only one element in the document?

允我心安 提交于 2021-01-29 09:17:48
问题 I want to updates only one element in the document to use put request I tried PUT localhost:3000/api/memo/5b8e382d61984255d879f872 { text: "updated!" { but, it updated like this { text: "updated!", imgUrl: null, tag: null, ... } Is there a way to updates one element in document? await Memo.where('_id') .equals(req.params.id) .updateOne({ imgUrl: req.body.imgUrl, text: req.body.text, tag: req.body.tag, user: req.body.user, loc: req.body.loc }) 回答1: You can use the following solution to update