Questions tagged [mongoose]

Mongoose is a MongoDB object modeling tool, or ODM (Object Document Mapper), written in JavaScript and designed to work in an asynchronous environment.

0
votes
0answers
6 views

Defending against XSS attack with mongoose

I am working on a project in node.js which uses mongoose to handle MongoDB. I want to defend against XSS attacks. What is the suggested way to do it? I found this: https://www.npmjs.com/package/...
0
votes
0answers
4 views

MongoDB documents expiring too soon (mongoose)

I set expiry time to 24 hours, but the documents expire after around 5-10 minutes (I haven't timed it exactly). What am I doing wrong? My schema: const collectionSchema = new mongoose.Schema({ ...
0
votes
2answers
10 views

Hi, I have 209580 objects in an array, how to save all at once in MongoDB using mongoose?

I have 209580 objects in an array, how to save all at once in MongoDB using mongoose?
0
votes
1answer
30 views

Returning Callback in Javascript

Taking inspiration from this answer http://stack.kinokrad-co.coma/21571589/9036255 I decided to copy this into my own code. I've found that it doesn't give any response. function getCallback(...
0
votes
2answers
25 views

Creating a mongoose Schema using a javascript object

This might seem like a really silly question, but is it possible to pass a js object instead of having to write the key value pairs during a Schema assignment? So, instead of let schema = new ...
0
votes
0answers
25 views

Add each respective id in an array of ids in order from one document to another document

I have followed this SO thread which more or less help me to accomplish some of the logic I need. Please refer to this thread for some of the explanation used in the code. Import CSV Using Mongoose ...
-1
votes
0answers
24 views

Returning Callback [duplicate]

Taking inspiration from this answer http://stack.kinokrad-co.coma/21571589/9036255 I decided to copy this into my own code. I've found that it doesn't give any response. function getCallback(...
0
votes
1answer
26 views

Create and insert data into Mongo database when npm install is run

I want to create a database and insert a collection of data. I'm using NodeJs and Mongodb with some help of Mongoose. I'm trying to run the script from the package.json in order to prevent the pain of ...
0
votes
0answers
14 views

Connect mongodb 4.0 with robomongo

Here are the logs for my mongodb status which show in it is in running status [email protected]:/etc$ sudo service mongod status ● mongod.service - MongoDB Database Server Loaded: loaded (/...
0
votes
0answers
23 views

Unable to push object into array in Mongodb collection but Id is inserted

I am using comment array in my schema as fallows. I want to push comments data into that comment array using nodejs api var Schema = mongoose.Schema; var myfeeds = new Schema({ title: { ...
0
votes
1answer
15 views

Removing An Object From Core Mongoose Array Before Pushing New Object Into It

I have a user model in which there is an array of tokens. Each token is an object having three key value pairs. I want to keep only one token object in tokens array with key 'access'='auth' .The code ...
1
vote
2answers
28 views

Update array filtering by 2 fields [duplicate]

I don't know if it is possible. I'm trying to do an automatic process to update all elements of a nested array in some documents. The array hasn't a fixed length. Below is a simplified example of ...
0
votes
1answer
19 views

Find Object that contains an unlimited number of properties, properties names and properties values

Having a mongoose Schema containing a property named 'attributes' of type Object. The 'attributes' Object can contain an unlimited number of properties, properties names and properties values. ...
0
votes
1answer
32 views

SignUp form with NodeJs mongodb(mongoose) express using passport

I am having a signup form with fields like username password email, i am using mongodb to store these data my schema looks like: var mongoose = require("mongoose"); var Schema = mongoose.Schema; ...
0
votes
1answer
23 views

Callback function fires in both success/error cases

I have a Nodejs/Express API using Mongoose and Mongo. I have a function that does an update. The update works, but for some reason, it also always logs an error. Here is the block of code I'm ...
1
vote
0answers
15 views

Mongoose wrapper class. Fails in production

I built a facade class for typical mongoose operations. It seem to work well in development and testing. When I packaged it up to be used in other development it failed. //defined as class ...
0
votes
3answers
24 views

Running Mongoose Query Only If It Returns A Value

I am trying to run a findByIdAndRemove-query. The program always takes the success path and I don't know why! I want it to take the response.status(500).send(errorMessage)-path if there is no record ...
1
vote
1answer
31 views

Search for multiple values in Mongoose at the same time [duplicate]

I have an array of values, such as: let items = ["abcd", "efgh", "ijkl", "mnop"] Sometimes there are a thousand items in this array, other times there are only a few. I also have a series of ...
1
vote
1answer
30 views

MongoDB Match $in on two different fields only when they are the same

Running into some issues with mongoDB query construction. I have an array of user IDs that I want to use to fetch posts. However, I only want to return posts for discussions that were created by a ...
0
votes
1answer
20 views

GET a string inside a object nested within a array

I have a get router that gets orders with the time stamp router.get('/api/order/:date/:time', function (req,res,next) { Box.find({orders: {date:req.params.date}}.then (function (order){ ...
0
votes
3answers
50 views

Pushing Mongoose Results into Array

I am trying to get results from my "Books.find" and push it into my books array. I want to then res.send it. I suspect this has something to do with some kind of asynchronous and scope rubbish. What'...
0
votes
2answers
15 views

Close connection when create connection mongoose

I'm used a multiple databases for every request , I createConnection to choose database and read/write data. But when everything is done how can I close this connection ? //create connection every ...
0
votes
0answers
15 views

Explain mongoose query using monk library

I'm using monk npm to explain query in mongoose. const mongoose = require('mongoose'); mongoose.set('debug', function(collection, method, query, doc) { if (method === 'ensureIndex') return; ...
0
votes
2answers
18 views

mongo query - Find the last 5 docs with a NOT as well

I am having a small issue at the moment with a query, in that it returns the "doc" that I expect to be excluded. db.offers.find({"offerImage1": { $exists : true },"offerStatus": {$not: {$ne: "...
1
vote
2answers
48 views

Javascript/NodeJS: html rendering before .find finishes inside forEach

This loop is supposed to be checking for opps in the DB that match opps being pulled from the salesforce api results and then either creating a new opp or finding the existing opp and pushing it to ...
0
votes
1answer
11 views

Populating an attribute that's an array of Ids using mongoose populate

I have an object that looks like this: Topic { "posts": [ "5baabfeb87a1401432791534", "5bac21814a0f9a2262a77f1b" ], "_id": "5ba06e74dbc05f039490438c", "topic": "new ...
1
vote
2answers
22 views

Querying with Mongoose by a field

I'm trying to query on this mongoose object 'Order' in express. However, when I add in the querystring variable between the parentheses on find, it doesn't work. I'm a bit lost on the documentation on ...
1
vote
2answers
49 views

MongoDB can't connect to Server

EDIT: I tried several different ports, killed tasks on ports and tried it again, it doesn't work. today i started a tutorial on the MERN stack, but i can't connect to the server. I get the following ...
1
vote
2answers
38 views

Delete Mongoose,Nodejs user at specific time

I have a question.How to delete mongoose and nodejs user at specifific time? Actually, I want the user to be deleted from the database within 60 days after I remove it.How I achieve this? I have ...
-1
votes
1answer
21 views

UnhandledPromiseRejectionWarning: undefined in Mongoose

When trying to authenticate users with Mongoose I get the following warning in my console: (node:20114) UnhandledPromiseRejectionWarning: undefined (node:20114) UnhandledPromiseRejectionWarning: ...
0
votes
0answers
22 views

How to Query Mongoose for a User that contains a specific ObjectID

So I have a userSchema that has a ref to a class model. I need to perform a query where I fetch a particular user that belongs to that class model. Currently I am fetching the entire class instance ...
0
votes
3answers
45 views

Query model with array in mongodb

I am new in node and MongoDB. I am trying to query a model with an array. The array looks like this var grArr = [ '5aabc39a3d88101e4b52c861', '5ac3a1fe83d45353bc6a995c' ] And the query is ...
0
votes
1answer
19 views

Mongoose - Multiple models for 1 schema

I am using mongoose v5.2.17. I was wondering is it possible to have multiple models map to the 1 schema. For example - I have the following model const mongoose = require('mongoose'); const validator ...
0
votes
0answers
10 views

Export model in mongoose or attach it universal app

MEAN stack, Let code do the talking: - app.js :- app.db = mongoose.createConnection(config.mongodb.uri); schema:- exports = module.exports = function(app, mongoose) { var adminSchema = new ...
1
vote
1answer
12 views

It is possible to pull elements from a referred objects' array using mongoose?

I have 2 mongo schemas related one with the other using ObjectId: var User = new Schema({ username: { type:String, unique: true }, password: { type:String }, ...
0
votes
0answers
23 views

MongooseJS populate from other Schema

So I have two Models: Tennis player (playerSchema) List of matches (eventSchema) First Model contains the following info: const playerSchema = new Schema({ name: String, matches: [{ ...
0
votes
1answer
20 views

How to add object to nested array in mongoose?

In essence what I am trying to do is something along the lines of FindByIdAndCreate, a method which does not exist in mongoose. I have a schema as so: const WordSchema = new Schema ({ ...
0
votes
1answer
25 views

Error: Can't set headers after they are sent NodeJS Mongoose?

I keep getting can't set headers after they are sent, i think the problem is because i am calling res.status(400).json 2 times, but i don't know how to do it other than this since i am new with node ...
0
votes
0answers
21 views

How to implement auto cancel order functionality mongodb

THE PROBLEM I have a restaurant orders collection (id,createdAt,updatedAt,state).state may hold any one of ["PENDING","PROCESSING","CANCELLED"] id,createdAt is indexed I wanted to change the state of ...
1
vote
1answer
25 views

Does Mongoose implicitly convert the type of a field when comparing in the collection as per the defined schema?

So I have a mongoose schema which has a following field: var orderSchema = new Mongoose.Schema({ status:{ type: Types.String, enum: ['0','2', '6', '9', '10', '11', '12'], ...
2
votes
2answers
29 views

I want to increment many documents in mongodb

router.put('/stockAccepted/:id', (req, res, next) =>{ stockSchema.findOneAndUpdate({_id: req.params.id}, { $set:{ stockAccepted: req.body.stockAccepted } },(...
0
votes
3answers
39 views

How to Improve Assignment Code in Node.js (ES6 style)

Is there any way to improve this code? Using ES6 style i am using latest stable version of node.js. const UserSchema = new mongoose.Schema({ date: string; dateObj: { year: number, month: ...
2
votes
1answer
47 views

Find with arrayFilters using Mongoose

I have to filter the object which contains only status C in comments(If atleast only comment have the status C then that object alone should be print) I tried using array Filters but I don't get ...
0
votes
1answer
11 views

Manipulating numerical document value from findById query

I am trying to manipulate the numerical value of a document as so, however when I console.log NewX it gives me NaN rather than the numerical value I am after. var OldX = await Col.findById(req....
1
vote
2answers
25 views

what is the purpose of declaring mongoose.Schema.Types.ObjectId?

I have been going through some nodejs/mongoose code and I realised that there are some codes which explicitly define the _id field when trying to store data in mongodb-mongoose. Example var ...
0
votes
1answer
25 views

What is the best way to handle querystrings in an express api?

I am a beginner nodejs developer, working on developing an express rest api with optional query params. For example consider the following schema for a user: phone: { countryCode: String, number:...
2
votes
1answer
33 views

$lookup multiple levels in mongodb

I'm new with mongodb and I have wrote query using mongoose populate. this.model.findById({ _id: req.params.id }) .populate('potentialLevels', 'description result plIndex') .populate({ path: '...
1
vote
1answer
30 views

Mongoose returns empty JSON array

Mongoose, express, MongoDB return an empty json array -- []. How can I fix this? Have installed all the packages. Have entered data into MongoDB using Robo 3t, so I think the problem is that ...
0
votes
1answer
24 views

How to do nested populate in mongodb?

router.get('/', (req, res) => { Post.find() .populate("author") .populate("comments") .populate("commentedBy") .sort({date : -1}) .exec() .then(posts => res.json(posts)); ...
0
votes
1answer
16 views

MongoDB Geospatial and createdAt sorting

I have a headache for a idea how to properly sort data from a MongoDB. It is using 2dsphere index and has timestamp createdAt. The goal is to show latest pictures (that what this collection is about, ...