These are chat archives for Automattic/mongoose

20th
Feb 2017
Paul "Joey" Clark
@joeytwiddle
Feb 20 2017 05:48

A couple of SO questions say you cannot get Mongo to sort string fields numerically.

Even during aggregation, Mongo cannot currently convert strings to numbers.

However some people have used mapReduce to perform the conversion. Maybe it’s possible to sort after performing a mapReduce? I don’t know.

Rob Sawyer
@robksawyer
Feb 20 2017 08:26
Anyone know how to pull off a category schema with keystone (uses mongoose)? The following doesn’t work.
PartCategory.add({
    name: { type: String, required: true, initial: true },
    images: { type: Types.Relationship, ref: 'Image', many: true, createInline: true },
    parent: { type: Types.Relationship, ref: 'PartCategory', createInline: true },
    numParts: { type: Number },
    ancestors: { type: Types.Relationship, ref: 'PartCategory', many: true },
    children: { type: Types.Relationship, ref: 'PartCategory', many: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'published', index: true }
});
James Lee
@uptownhr
Feb 20 2017 23:36
somewhat related to, Automattic/mongoose#2772
anyone know if you are able to populate a document that references it's own sub documents?
const nodeSchema = new Schema({
  label: String,
  description: String,
  intro: introSchema
})

nodeSchema.add({
  childs: [{ type: Schema.Types.ObjectId, ref: 'Node'}],
  parent: { type: Schema.Types.ObjectId, ref: 'Node'}
})



const treeSchema = new Schema({
  nodes: [nodeSchema]
})
t = new Tree()
t.nodes.push({
      label: 'a'
    })

    t.nodes.push({
      label: 'b'
    })

    t.nodes.push({
      label: 'c',
      parent: t.nodes[0]._id
    })

    t.populate({
      path: 'nodes',
      populate: {
        path: 'parent childs'
      }
    }).execPopulate().then( console.log )
i was hoping this would populate that node refs
but no luck
is there a way to connect these or must the refs be another collection?