mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Excluding fields in nested object not working

Open cosminn777 opened this issue 8 years ago • 1 comments

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Excluding fields in nested objects not working, ex: .select('-testNested.tags') will return results with testNested.tags. This happens when using a separate collection for the nested objects.

If the current behavior is a bug, please provide the steps to reproduce.

Code example below.

What is the expected behavior?

Results should not contain the excluded fields.

Please mention your node.js, mongoose and MongoDB version. MongoDB 4.0 Mongoose 5.2.1 Node 10.5.0 Windows 10

Code:

#!/usr/bin/env node --no-deprecation
'use strict';

const mongoose = require('mongoose');
// mongoose.set('debug', true);
mongoose.connect('mongodb://user:[email protected]:port/test', {
    retryWrites: true,
    authSource: 'admin',
    useNewUrlParser: true,
});
const conn = mongoose.connection;
var nestedSchema = mongoose.Schema({
    name: {type: String, index: true, required: true},
    tags: {type: [String], index: true, default: [], required: true},
    tagsb: {type: [String], index: true, default: [], required: true},
}, {
    timestamps: true,
    strict: true
});

var TestNested = conn.model('testNested', nestedSchema);
const objectSchema = mongoose.Schema({
    testNested: {type: mongoose.Schema.Types.ObjectId, ref: 'testNested', required: true, index: true},
}, {
    timestamps: true,
    strict: true
});
var Test = conn.model('test', objectSchema);

async function run() {
    await conn.dropDatabase();
    const test = new Test({
        "testNested": "5b41fb6285d236e72fe04740"
    });
    await test.save();
    const testNested = new TestNested({
        "_id": "5b41fb6285d236e72fe04740",
        "tags": [
            "tagA",
            "tagB"
        ],
        "tagsb": [
            "tagA",
            "tagB"
        ],
        "name": "Nested Name"
    });
    await testNested.save();
    Test.find({}).sort({}).limit(100).populate('testNested').select('-testNested.tags -testNested.tagsb').lean(true).exec(function (err, res) {
        console.log(err, res);
    });
}

run();

And the output is:

null { _id: 5b41fe8182820d4b981eecca,
  testNested:
   { _id: 5b41fb6285d236e72fe04740,
     tags: [ 'tagA', 'tagB' ],
     tagsb: [ 'tagA', 'tagB' ],
     name: 'Nested Name',
     createdAt: 2018-07-08T12:07:29.953Z,
     updatedAt: 2018-07-08T12:07:29.953Z,
     __v: 0 },
  createdAt: 2018-07-08T12:07:29.925Z,
  updatedAt: 2018-07-08T12:07:29.925Z,
  __v: 0 }

Expected return: testNested object without tags and tagsb

null { _id: 5b41fe8182820d4b981eecca,
  testNested:
   { _id: 5b41fb6285d236e72fe04740,
     name: 'Nested Name',
     createdAt: 2018-07-08T12:07:29.953Z,
     updatedAt: 2018-07-08T12:07:29.953Z,
     __v: 0 },
  createdAt: 2018-07-08T12:07:29.925Z,
  updatedAt: 2018-07-08T12:07:29.925Z,
  __v: 0 }

I want to mention that I can exclude tags and tagsb by using:

.populate({
        path: 'testNested',
        select: '-tags -tagsb -_id'
    })

And the following is also not working as expected (thowing error):

.select({
         testNested: {
             tags: 0,
             tagsb: 0
         }
     })

However, the code provided in example works if I am selecting the objects from the same collection where testNested is actually a nested object in test. A single collection where I have objects like:

test: {
  testNested: {
        "tags": [
            "tagA",
            "tagB"
        ],
        "tagsb": [
            "tagA",
            "tagB"
        ],
        "name": "Nested Name"
    }
}

PS. Thanks @lineus for your support!

cosminn777 avatar Jul 08 '18 13:07 cosminn777

Unfortunately we don't support select() on populated paths. You need to pass the paths you want to exclude to populate():

    Test.find({}).sort({}).limit(100).populate('testNested', '-tags -tagsb').lean(true).exec(function (err, res) {
        console.log(err, res);
    });

vkarpov15 avatar Jul 11 '18 14:07 vkarpov15