These are chat archives for Automattic/mongoose

17th
Jul 2018
Simon Bailey
@thebailers
Jul 17 2018 11:04

Hey all, does anyone know hoe to prevent Mongoose from saving an ObjectId to mLab as an $oid:

"owner": {
        "$oid": "5b43bdca6ca3de2be644c04d"
    },

This is the contoller and the model:

const router = require('express').Router()
const VerifyToken = require('../auth/VerifyToken')
const shortid = require('shortid')

const mongoose = require('mongoose')
const ObjectId = mongoose.Types.ObjectId

// model
const Household = require('./Household')

router.post('/create', VerifyToken, (req, res) => {

  const household = new Household({
    householdName: req.body.householdName,
    shortCode: shortid.generate(),
    owner: ObjectId(req.userId),
    members: [req.userId]
  })

  household.save((err, household) => {
    console.log(err)
    if (err) return res.status(500).send('There was a problem creating your household.')

    res.status(200).send('Household created successfully')
  }) 
})

module.exports = router
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId

const householdSchema = new Schema({
  householdName: {
    type: String,
    required: true
  },
  shortCode: {
    type: String,
    required: true
  },
  owner: {
    type: ObjectId,
    required: true
  },
  members: [{
    type: ObjectId
  }]
})

const Household = mongoose.model('household', householdSchema)

module.exports = Household
Kev
@lineus
Jul 17 2018 11:06
@thebailers the owner path in your schema is type: ObjectId, if you'd rather store a string, make it type: String
though, I'm not sure where the $oid part comes from :/
Simon Bailey
@thebailers
Jul 17 2018 11:07
@lineus But I thouight storing it as an ObjectId was correct
When I create it, it is: owner: ObjectId(req.userId),
Kev
@lineus
Jul 17 2018 11:08
where does ObjectId() come from?
oh, I see
Simon Bailey
@thebailers
Jul 17 2018 11:08
Mongoose.Schema.Types.ObjectId
Kev
@lineus
Jul 17 2018 11:09
where did this come from:
"owner": {
        "$oid": "5b43bdca6ca3de2be644c04d"
    },
Simon Bailey
@thebailers
Jul 17 2018 11:10
That is from the mLab database
that is what the /create POST is creating in the db
Kev
@lineus
Jul 17 2018 11:11
did you insert that particular document via the mlab ui?
what connection options are you passing to mlab?
Simon Bailey
@thebailers
Jul 17 2018 11:13
via postman
connection options? not sure what you mean
Kev
@lineus
Jul 17 2018 11:14
mongoose.connect(url, options)
Simon Bailey
@thebailers
Jul 17 2018 11:14
ahh
no options
mongoose.connect(keys.mongodb.dbURI, () => console.log('Connected to database'))
Kev
@lineus
Jul 17 2018 11:16
ok cool
this is a new one on me. give me a few minutes to do some research :smile:
Simon Bailey
@thebailers
Jul 17 2018 11:16
ah man thank you so much
Kev
@lineus
Jul 17 2018 11:18
just for grins, try not explicitly calling ObjectId(), let mongoose handle the cast to ObjectId for you and see what happens.
Simon Bailey
@thebailers
Jul 17 2018 11:20
which call to ObjectId() shpuld I remove?
In the model? Or the creation?
of the document
Kev
@lineus
Jul 17 2018 11:21
in the creation.
is req.userId a 24 char hex string?
Simon Bailey
@thebailers
Jul 17 2018 11:21
yeah
Kev
@lineus
Jul 17 2018 11:24
I just looked at the test collection I created the only other time I've used mlab, and the documents I created in there have the same '$oid' for _id.
Simon Bailey
@thebailers
Jul 17 2018 11:25
ahhh weird
Yeah letting mongoose cast the ObjectId results in the same
quirk of mlab then?
Queries still work the same when querying the ObjectId as if it wasn’t nested in $oid
Kev
@lineus
Jul 17 2018 11:26
yeah definitely, trying to find a way to disable it
it must be transparent to mongoose, or I would have noticed it last time I used it, iirc all of my queries,updates, etc worked.

BUT I DON'T LIKE IT!

Simon Bailey
@thebailers
Jul 17 2018 11:27
yeah I don’t like it either, lol
looks unclean
Simon Bailey
@thebailers
Jul 17 2018 13:50

Hey all, if I have a schema whereby an expense has a split - which is an array of objects, each object has a member ObjectId value and an amount of the split:

split: [{
    member: {
      type: ObjectId,
      required: true,
    },
    amount: {
      type: Number,
      required: true
    }
  }],

How do I map over that request data in the controller what I want to save the data to the db?

const expense = new Expense({
      household: household._id,
      split: {
        // loop? req.body.split.map?? 
      }

    })
Kev
@lineus
Jul 17 2018 13:57
If I understand your question @thebailers, the answer is yes. a simplified example
but I think your syntax is a little off in your creation code, it should be split: req.body.split.map(..)
Simon Bailey
@thebailers
Jul 17 2018 13:59
Thanks @lineus that’s great
Kev
@lineus
Jul 17 2018 13:59
glad I could actually help this time lol
Simon Bailey
@thebailers
Jul 17 2018 14:01
haha :)