mongoose

How to use Jest to mock and spy a `mongoose.connect` in a provider of NestJS

試著忘記壹切 提交于 2021-01-28 12:03:06
问题 I defined a custom database-specific Module to connect a MongoDB via Mongoose APIs. The complete code is here. { provide: DATABASE_CONNECTION, useFactory: (dbConfig: ConfigType<typeof mongodbConfig>): Connection => createConnection(dbConfig.uri, { useNewUrlParser: true, useUnifiedTopology: true, //see: https://mongoosejs.com/docs/deprecations.html#findandmodify useFindAndModify: false }), inject: [mongodbConfig.KEY], }, When writing tests for this provider, I want to confirm the database

Can't save createdAt and updatedAt as datetime values nor backend neither frontend

前提是你 提交于 2021-01-28 11:51:39
问题 I'm trying to save datetime values in TodoForm but those values are not reflected in database. This is my todo model. Todo.js const mongoose = require('mongoose'); const { Schema } = mongoose; const todoSchema = new Schema({ name: String, description: String, isDone: Boolean, createdAt: Date, updatedAt: Date }, {timestamps: {createdAt: 'created_at' updatedAt: 'updated_at'}} ); mongoose.model('todos', todoSchema); Then Follows my todosController post route. TodosController: app.post('/api

How to Populate data using Mongodb Aggregate framework?

不打扰是莪最后的温柔 提交于 2021-01-28 11:32:30
问题 I have a current MongoDB Aggregate framework pipeline from a previous question and I am unable to add populate query to grab user profile by id. My code is below Product.aggregate([ { $group: { _id: { hub: "$hub", status: "$productStatus", }, count: { $sum: 1 }, }, }, { $group: { _id: "$_id.hub", counts: { $push: { k: "$_id.status", v: "$count", }, }, }, }, { $group: { _id: null, counts: { $push: { k: { $toString: "$_id" }, v: "$counts", }, }, }, }, { $addFields: { counts: { $map: { input: "

Bad Request when registering with passport

非 Y 不嫁゛ 提交于 2021-01-28 10:47:27
问题 I am trying to build an authentication panel for the MEAN stack using PassportJS. I have the following code for registering new users with email (instead of the default username) and password: router.post("/register", function (req, res) { var newUser = new User({ username: req.body.email }); User.register(newUser, req.body.password, function (err, user) { if (err) { return res.render('account/signup'); } passport.authenticate("local")(req, res, function () { res.redirect("/account/profile");

Mongoose custom validation for password

北慕城南 提交于 2021-01-28 08:09:47
问题 I am trying to make Schema using mongoose and stuck in a point on how to apply custom validation for the password, where password contains: one special character password should have one lowercase and one uppercase character password should have a length of more than 6 Here is the Schema: const mongoose = require('../db/mongoose'); const validator = require('validator'); const UserSchema = new mongoose.Schema({ email: { type: String, validate: { validator: validator.isEmail() } }, password: {

How to validate object keys and values in Mongoose Schema?

纵饮孤独 提交于 2021-01-28 07:30:47
问题 In my Mongoose Schema I'm trying to simulate a dictionary offersInCategory that looks like this: offersInCategory = { "Electronics": 2, "Furniture": 5 }; Mongoose doesn't support dictionaries so I'm forced to use object literals in an array instead, like so: offersInCategory: [{ category: { type: String, enum: ['Furniture', 'Household', 'Electronicts', 'Other'] }, val: { type: Number, min: 0 } }] My problem with this approach is it feels unintuitive. Furthermore it doesn't prevent my model

Model.find() returns null after model.save() in Mongoose + Nodejs

寵の児 提交于 2021-01-28 06:43:13
问题 The save function works perfectly but, the find function just below it returns null on searching the save object which is previously saved. Here is the code: var mongoose = require("mongoose"); var User = mongoose.model("User"); module.exports.register = function(req, res) { var user = new User(); user.name = req.body.name; user.email = req.body.email; user.setPassword(req.body.password); user.save(function(err) { var token; token = user.generateJwt(); res.status(200); res.json({ token: token

Match Two different fields in Mongoose, Aggregate?

女生的网名这么多〃 提交于 2021-01-28 06:40:19
问题 I'm trying to match two different fields in the same document. But didn't get expected output as I want. Let me show with an example. I want to match weighted.phaseId with phases._id in same documents and not match should be removed from phases fields. Does any one have an Idea ? // Document after processing some aggregate query over a database. { "_id" : ObjectId("5a680c803096130f93d11c7a"), "weighted" : [ { "phaseId" : ObjectId("5a6734c32414e15d0c2920f0"), "_id" : ObjectId(

check if a field value exit in array - MongoDB

烈酒焚心 提交于 2021-01-28 06:32:51
问题 I'm using MongoDB and i have the following schema of person collection: Person { age: [number] } i want to check if the age of a person exist in array, for exemple [15, 20, 12, 0]. i need something like $in operator but in reverse. schema: initiatives : { ressources: [ { departement: ObjectId } { departement: ObjectId } ] ) 回答1: You can use $expr with $in: Person.find({ $expr: { $in: [ "$age", [15, 20, 12, 0] ] } }) EDIT: to compare arrays you need $setIntersection and $size operators, try:

Mongoose schema option change doesn't reflect in database

陌路散爱 提交于 2021-01-28 06:17:50
问题 I have a mongoose schema defined like the following one: { Username: { type: String, unique: true, required: true, }, Password: { type: String, required: true, minlength: 10, maxlength: 20 } } For example after we launch the production build of our app which is running live, if I want to change "unique" to unique: false for the "Username" how should I do it? So on my machine when the server is running i created a User with Username and Password, mongo created User for me, now i changed the