These are chat archives for Automattic/mongoose

27th
Jan 2016
J
@jhyland87
Jan 27 2016 02:38
is it possible to reference other models from within model methods?..
J
@jhyland87
Jan 27 2016 02:44
When I try to load the other model inside the model I need to use it in, I get an error saying I cant overwrite it, so its there somewhere, I think.
Ah, got it:
module.exports = Mongoose => {
    const Asset = Mongoose.models.Asset
Konstantin Baierer
@kba
Jan 27 2016 10:39
@alfredopacino You need to use a proper sub-schema in your array definition
@alfredopacino : Something like (untested):
var reg_schema = new Schema({
  number: Number,
  year: Number
});
var com_post_schema = new Schema({
    content: { type: String, required: true },
    reg:[reg_schema],
    postedBy: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'com_user'
    }]
});
Pete Redmond
@httpete-ire
Jan 27 2016 15:33

I'm trying to ensure that a new docs document has to be unique by both its _title and _owner id, below is the Schema for my document.

let DocsSchema = new Schema({

  title: {
    type: String,
    required: true,
    index: true
  },

  desc: {
    type: String
  },

  created: {
    type: Date
  },

  updated: {
    type: Date
  },

  owner: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    index: true
  },

  chapters: [
    Chapter.schema
  ],

  published: {
    type: Boolean,
    default: false
  }

});

// ensure that a document is unique by the 'title' and 'user'
DocsSchema.index({
  title: 1,
  owner: 1
}, {
  unique: true
});

This does not seem to work as I can create docs with the same title, as anyone got any suggestions? Cheers

Konstantin Baierer
@kba
Jan 27 2016 16:14
@httpete-ire Are you sure the indexes are created? Check the mongo CLI client. Have you restarted Mongo? Has Mongoose emitted the "index" event? Also, if you aren't doing more complex indexing, consider setting unique on the individual schema fields
Steve Babigian
@k00k
Jan 27 2016 16:17
Anyone know if there’s a way to force a schema type of array on a property to always actually be an Array? What I mean is, if I insert a single item (string) into a property designated as Array in my schema, it inserts as a string due to [] or Array’s mixed nature.
Konstantin Baierer
@kba
Jan 27 2016 16:19
@k00k Why does it have to be an array of strings? Why not an array of sub schema with a single field that contains the individual value? Then you can use Mongoose's statics and methods for validation etc.
Steve Babigian
@k00k
Jan 27 2016 16:20
@kba ok, i will try that, thanks
Pete Redmond
@httpete-ire
Jan 27 2016 16:51
@kba I'm using the RoboMongo GUI, and I see a few indexes including title_1 _owner_1. Does this look right?
@kba Also when I restart Mongo I see the 'index' event, but now the DB only accepts one document, it returns a 11000 error for every other create operation
Konstantin Baierer
@kba
Jan 27 2016 16:57
@httpete-ire title_1 and owner_1 look right.
11000 is the error code for unique constraint violation, right?
If it only allows one document regardless even if title or owner differ, make sure, the _id is unique.
Pete Redmond
@httpete-ire
Jan 27 2016 17:00
@kba is _id not uniques by default?

What I am trying to achieve is

User: 1 can have a document with the title Intro
User 2 can also have a document with the title Intro

but User: 1 can not create another document with the title Intro
At the moment I can only create one document and then it returns 11000 no matter what user id I use
Konstantin Baierer
@kba
Jan 27 2016 17:08
@httpete-ire Might be a stale index? Try removing the index/dropping the collection?
Pete Redmond
@httpete-ire
Jan 27 2016 17:23
@kba Dropped the collection, restarted my machine and mongo and didnt work
will only allow me to enter one document regardless of title or owner id
Konstantin Baierer
@kba
Jan 27 2016 17:26
if it's a unique violation, mongo should output more info. Which key does it complain about?
e.g. something like {"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1 dup key: { : null }"}
Pete Redmond
@httpete-ire
Jan 27 2016 17:28
I get this
code: 11000,driver: true,
errmsg: "E11000 duplicate key error index: docd.docs.$chapters._id_1 dup key: { : null }"getOperation: () {arguments: (...)get arguments: ThrowTypeError() { [native code] }set arguments: ThrowTypeError() { [native code] }caller: (...)get caller: ThrowTypeError() { [native code] }set caller: ThrowTypeError() { [native code] }length: 0name: ""prototype: WriteError.getOperation__proto__: Empty() {}<function scope>index: 0message: "E11000 duplicate key error index: docd.docs.$chapters._id_1 dup key: { : null }"name: "MongoError"
Konstantin Baierer
@kba
Jan 27 2016 17:28
docd.docs.$chapters._id_1 dup key: { : null }
there's your problem, no _id
Pete Redmond
@httpete-ire
Jan 27 2016 17:30
chapters is an embedded collection of documents
So my mistake is I am trying to index an embedded document??
Here 's the chapter schema
var ChapterSchema = new Schema({

  _id: {
    type: String,
    unique: true,
    index: true,
    default: shortid.generate
  },

  ...

});
Konstantin Baierer
@kba
Jan 27 2016 17:33
the problem is that _id is null in at least one case.
are you sure shortid.generate gets fired and returns something?
@httpete-ire or just leave off the whole _id part and let Mongoose create one as it does by default.
Pete Redmond
@httpete-ire
Jan 27 2016 17:55
@kba thanks for your help, letting Mongoose create the _id seem to do the trick
J
@jhyland87
Jan 27 2016 19:29
Hey guys, How do I populate some referenced fields, when they reference document _id's in a sub-schema?