These are chat archives for Automattic/mongoose

14th
Sep 2018
ratnesh2581990
@ratnesh2581990
Sep 14 2018 14:02

Hi there I am facing problem in running populate query with mongoose
here is my model which i am using

let mongoose = require('mongoose');
var Schema = mongoose.Schema;
let orderSchema = Schema({
    orderid: {
        type: Number,
        required: true
    },
    customername: {
        type: String,
        required: true
    },
    brokername: {
        type: String,
        required: true
    },
    customernumber: {
        type: Number
    },
    mobilebumber: {
        type: Number
    },
    brokernumber: {
        type: Number
    },
    amount: {
        type: Number,
        required: true
    },
    transportcharge: {
        type: Number,
        required: true
    },
    orderdate: {
        type: Date,
        required: true,
        default: Date.now
    },
    ordermilisecond: {
        type: Number,
        required: true
    },
    ordernote: {
        type: String
    },
    cartobject:{
        type: Array
    },
    userid: {
        type: Schema.Types.ObjectId, ref: 'users',
        required: true
    }
});

let Order = module.exports = mongoose.model('Order', orderSchema);

Here is my code to run that query

const express = require('express');
const router = express.Router();
const moment = require('moment-timezone');
let Order = require('../models/order');
let User = require('../models/user');

router.get('/all', ensureAuthenticated, function (req, res) {
    Order.
find({}).
  populate('userid').
  exec(function (err, user) {
    console.log('The userid is %s', user);
  });
});

But still i am getting undefine in console here is my schema for user collection

const mongoose = require('mongoose');

// User Schema
const UserSchema = mongoose.Schema({
  name:{
    type: String,
    required: true,
  },
  email:{
    type: String,
    required: true
  },
  station: {
    type: String
  },
  accountnumber: {
    type: Number
  },
  phonenumber:{
    type: Number,
    required: true,
    minlength: 10,
    unique: true,
    trim: true
  },
  password:{
    type: String,
    required: true,
    minlength: 6
  },
  role:{
    type: String,
    required: true
  },
  myreferencecode:{
    type: String,
    required: true
  },
  referedcodeused:{
    type: String
  },
  referalamout:{
    type: Number
  },
  status: {
    type: String
  },
  discountbyrefnumber:{
    type: Number
  }
});

const User = module.exports = mongoose.model('User', UserSchema);

Please help me to find where i am doing wrong

Kev
@lineus
Sep 14 2018 14:40
@ratnesh2581990 is user in your example undefined? It should be an array of Order documents, or at least an empty array.
and the ref defined in the userid path of orderSchema, should be the same as the string in the User model, User instead of users
if you had an error handler in that callback to find(), it would probably say something like no model for users
Order.
find({}).
  populate('userid').
  exec(function (err, user) {
    if (err) { return console.error(err); }
    console.log('The userid is %s', user);
  });
mushfau
@mushfau_twitter
Sep 14 2018 15:54
merchantSchema.pre('save', function (next) {
    let doc = this;
    Counter.findOneAndUpdate({}, {$inc: {merchants: 1}}, {new: true}, function (err, count) {
        if (err) {
            return next(err);
        } else {
            if (doc.isNew) {
                doc.mid = '18RH' + count.merchants.toString().padStart(6, '0'); // 18RH000001
            }
            next();
        }
    });
});

hi guys... i have model which has a field 'mid' which needs to auto-increment (it is working) but i need the prefix to be based on the current year, and every year the id needs to restart from 1.

if my last mid in 2018 is 18RH000875, the next one which get generated in 2019 should be 19RH000001
whats the simplest way to achieve this?

thanks :)

locokamikaze
@locokamikaze
Sep 14 2018 17:45

Hi, small question regarding using Mongo with Scala query... how can I check if Seq of string match the mongo values:

def getShipmentsByStatusAndType(status:String, `type`: Seq[String])=
    findAll($and("status" $eq status, "type" $in `type`), sort = BSONDocument("created" -> -1))

I tried to use toList or toArray but getting no answer back - there's a values in the DB but I get none.
Do I need to change the Seq var? or maybe to use something else and not $in?

Kev
@lineus
Sep 14 2018 18:32
@locokamikaze this is a channel for mongoose, the javascript library that interacts with mongodb's native js driver. You might try the mongodb slack channels for someone to help in person.
locokamikaze
@locokamikaze
Sep 14 2018 18:35
tnx
Kev
@lineus
Sep 14 2018 18:35
@locokamikaze I was just looking at the docs for scala to see if I can make sense of that query
or see anything off
@locokamikaze are you using the native scala driver? where does findAll come from?
locokamikaze
@locokamikaze
Sep 14 2018 18:44
@lineus thanks for the help - findAll is part of the reactivemongo.bson_ library (BsonDao.scala)
Kev
@lineus
Sep 14 2018 19:00
@locokamikaze i'm not finding a whole lot of options for docs about the syntax. Maybe it's time to join the dark side and start using node.js and mongoose :)
locokamikaze
@locokamikaze
Sep 14 2018 19:04
:joy: thanks - maybe in the future
stherrienaspnet
@stherrienaspnet
Sep 14 2018 19:39
Hello everyone, i need help to figure out how do you manage translations (i18n) with mongoose?
Kev
@lineus
Sep 14 2018 20:07
@stherrienaspnet someone recently suggested https://github.com/i18next/i18next in the slack channel.