mongoose

How to get a multiple result in mongoose and combine it in one single response

我只是一个虾纸丫 提交于 2021-01-28 05:21:13
问题 In a API(Route) call, I wish to have 3 mongoose query and then combine results to form a response json. Query student .countDocuments ( {} ) .then(stundentNumber => { return stundentNumber }) teacher .countDocuments ( {} ) .then(teacherNumber => { return teacherNumber; }) staff .countDocuments ( {} ) .then(staffNumber => { return staffNumber; }); Desired Response res.json({ teacher: teacherNumber, student: stundentNumber, staff: staffNumber }); How can it be done using nodejs/mongoose 回答1: If

Mongoose model not persisting object

一曲冷凌霜 提交于 2021-01-28 05:13:44
问题 In a nutshell, I'm working on a math assessment app that takes your answers and stores them in a database and I'm having trouble adding more than one object to the backend. The Mongoose model is as such: const mongoose = require('mongoose'); const Algebra1Schema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }, answers: { type: Object }, date: { type: Date, default: Date.now } }) module.exports = algebra1 = mongoose.model('algebra1', Algebra1Schema) Here is

Unique index ignored when updating with Mongoose + Mockgoose in NodeJS

随声附和 提交于 2021-01-28 05:00:45
问题 I'm currently using Mongoose ODM to manage database connections to MongoDB in a NodeJS application, and intercepting the connection using Mockgoose in Mocha tests. I've ran into an issue where my unique indexes are being ignored when performing an update to a document. I'm simply wrapping Mongoose with another package called Mongoose-bird, which just enables the use of promises. One schema in particular is as follows: // Gallery.js 'use strict'; var mongoose = require('mongoose-bird')(require

How do I query a set of objects with an array of values in mongoose?

耗尽温柔 提交于 2021-01-28 03:41:41
问题 I have a schema like this const rankSchema = new Schema( { rank: { type: Object, default: {} }, lastUpdated: { type: Date, default: Date.now() }, }, { minimize: false } ); And my database has an object 'rank' with many other objects inside of it like this. rank: { Person1: { Stat1: 2, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 4 }, Person2: { Stat1: 4, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 2 }, Person3: { Stat1: 1, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 1 }, Person4: { Stat1: 2, Stat2: 0, Stat3: 0,

How to get data from array in mongoose?

允我心安 提交于 2021-01-28 03:30:36
问题 I am new to mongoose node.js and mongoDB, I have a db Schema like Project:{ projectName:"String", projectManager:"String", task:[{ taskName:"String", timetakeninhrs:"String" }] }; So what I want is to get only the details of task with particular task name. I am writing sql script so that you can know what I want : Select taskname,timetakeninhrs from project where taskName ='DB create'; 回答1: The $elemMatch projection operator would come in handy for this: Project .where('task.taskName', 'DB

mongodb aggregate lookup with a query

断了今生、忘了曾经 提交于 2021-01-28 02:59:41
问题 I have collections with following values: reports { "_id": { "$oid": "5f05e1d13e0f6637739e215b" }, "testReport": [ { "name": "Calcium", "value": "87", "slug": "ca", "details": { "description": "description....", "recommendation": "recommendation....", "isNormal": false } }, { "name": "Magnesium", "value": "-98", "slug": "mg", "details": { "description": "description....", "recommendation": "recommendation....", "isNormal": false } } ], "patientName": "Patient Name", "clinicName": "Clinic",

mongoose geojson in schema, “Can't extract geo keys” error

狂风中的少年 提交于 2021-01-28 02:53:57
问题 I have a mongodb collection with a defined schema, and I updated this schema to include lat/lon coordinates. old version: var schema = mongoose.Schema({ id: String, name: String, address: String, city: String, zip: String, country: String, phoneNumber: String, mobile: String, website: String, email: String, }); new version var schema = mongoose.Schema({ id: String, name: String, address: String, city: String, zip: String, country: String, phoneNumber: String, mobile: String, website: String,

How to lookup a field with an array in nested subdocument mongodb?

眉间皱痕 提交于 2021-01-27 22:57:57
问题 I am trying to retrieve some lookup data for an embedded array in a document. Here is a sample of the data: { "_id": "58a4fa0e24180825b05e14e9", "fullname": "Test User", "username": "testuser" "teamInfo": { "challenges": [ { "levelId": "5e14e958a4fa0", "title": "test challenge 1.1" }, { "levelId": "5e14e958a4fa0", "title": "test challenge 1.2" }, { "levelId": "5e14e958a4fa1", "title": "test challenge 2.1" } ] } } As you see, teamInfo.challenges is an array, containing levelId fields. These

Row count per each distinct value of the field in Mongoose

﹥>﹥吖頭↗ 提交于 2021-01-27 22:00:44
问题 I have a schema like { reactionType: String, userName: String } (really simplified), and I want to count how many reactions of each type a given user has got. How do I do this with Mongodb and Mongoose? I tried doing MyModel.find({ userName: 'myUser' }).distinct('reactionType').count() and the variations of this, but this seems to count the reaction types present for a given user, not the count of each reaction type for the user. In case this helps to clarify what I want, in SQL I'd do

I cant map array of objects eventhough it's printed in console.log

邮差的信 提交于 2021-01-27 21:14:32
问题 I'm trying to map an array of objects but it keeps saying that docs are undefined even though I can clearly see it in console.log This is what I get when I console.log {console.log(this.props.results.data)} what I want is in the docs field. If I expand it looks like this Since I want to print out the content of docs this is what I tried doing <div className='list-render'> {this.props.results.loading ? <LoadingSpinner/> : <div className='row'> {(this.props.results.data.docs.map((data) => { //