These are chat archives for Automattic/mongoose

Oct 2015
Dung Tran Van
Oct 26 2015 00:38
pls! help me query mongodb. :worried:
I have model chat
I want query get last msg each conversation , same as facebook conversation.
Thanks all :D
Adeola Awoyemi
Oct 26 2015 00:45
Hi, I'm trying to use mongoosejs Query middleware but I don't see the query being run. Does anyone know more about this and able to help?
John Farrow
Oct 26 2015 08:33

Hello people. Struggling with a plugin concept, can anyone point me in the right direction?

I want to be able to add static methods to models dynamically. I want to add methods to findBy{PathName}

function findByCurry(key) {
        return function(value, cb) {
            var model = this.model(this.constructor.modelName);
            return model.find({
                key: value
            }, cb);

    module.exports = function(schema) {
        var keys = Object.keys(schema.paths).filter((key) => {return !!key[0].match(/[a-zA-Z]/)});
            titleKeys = => {return key.replace(/\w/, function(txt) {return txt.charAt(0).toUpperCase()});});

        keys.forEach((key, i) => {
            schema.statics['findBy' + titleKeys[i]] = findByCurry(key);

Currently, the method is not being attached to the model instance. I suspect it's something to do with the forEach, but I can't be sure because nothing seems to get console logged at any point within the plugin.

The idea is to dynamically assign findBy methods to each scheme path, but the methods are unattached. :(
Pankaj Bhanu
Oct 26 2015 11:47
is it possible to register a static method to a model once the model is created. example below
var User = mongoose.model('User');
User.schema.statics.create = function();
Richard Brookfield
Oct 26 2015 13:40
@vkarpov15 Thank you!
Diego Aguilar Aguilar
Oct 26 2015 15:55
Hello, I have a method like this returning a promise:
function getRandomVideos (twoDimensions) {

    var coordinates = twoDimensions ? [_.random(-180,180), _.random(-90,90)]  : [_.random(-180,180), 0];

  return Video.find(
        "randomTag": {
          "$near": {
            "$geometry": {
              "type": "Point",
              "coordinates": [_.random(-180,180),0]
        {youtubeId: true, title: true, coordinates: true, parts: true},
        {limit: 3})
Then I want to use it and being capable of catching a possible error:
            .then(function (videos) {
                response.render('random', {randomVideos: videos});
            .then(function (null,err) {
I tried first with classic .catch but wouldn't work
that's correct way? Btw I'm using 4.2 version
James Lee
Oct 26 2015 19:19
@vkarpov15 thoughts on why the new flag defaults to false for findbyidandupdate?
Matthew Bonig
Oct 26 2015 19:45
hey all... I've got a virtuals problem, I think... I have an object, SomeObject, and I want there to be a field on it, .someValue. That .someValue is NOT persisted to the db but calculated by middleware after retrieval. However, it's not showing up on the client.
despite being set.
1st question: can I create a virtual field on the schema that has no setter/getter (and is just a regular field) and second, how do I get that to the client. I have the schema setup with ... Schema({...},, {
toObject: {
virtuals: true
toJSON: {
virtuals: true
but that doesn't seem to work
Matthew Bonig
Oct 26 2015 19:59
k, I've even tried a step further without any luck....
var MessageSchmea = Schema({...}, {
    toObject: {
        virtuals: true
    toJSON: {
        virtuals: true
MessageSchema.virtual("myVote").get(function(){return this.__myVote;}).set(function(v){this.__myVote = v;});
Matthew Bonig
Oct 26 2015 20:06
alright.. .nm.... mongoose was doing everythign right. Turns out it was serializing the response before my values were getting added