Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{$exists: false} not working for nested fields in $pull statement #8166

Closed
JonathanCallewaert opened this issue Sep 15, 2019 · 1 comment
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@JonathanCallewaert
Copy link

JonathanCallewaert commented Sep 15, 2019

I have a collection documents with a document that looks like this:

{
    "_id" : ObjectId("5d7ddedf92264a43ceee9f48"),
    "name" : "test123",
    "predictedEntityValues" : [ 
        {
            "status" : "DONE",
            "values" : [ 
                {
                    "text" : "123"
                }
            ]
        }
    ]
}

I have the following query where documentIds = [ ObjectId("5d7ddedf92264a43ceee9f48") ]

await models.documents
    .updateMany(
      { _id: { $in: documentIds } },
      {
        $pull: {
          predictedEntityValues: { 'values.0.coords': { $exists: false } },
        },
      }
    )

The above query should should remove the value in the array 'predictedEntityValues', but it doesn't.
My query is correct because when I execute it into the mongo shell, it works.

I turned on debug mode, and I saw that the library parsed my query to this query:

Mongoose: documents.updateMany({ _id: { '$in': [ ObjectId("5d7ddedf92264a43ceee9f48") ] } }, { '$setOnInsert': { createdAt: new Date("Sun, 15 Sep 2019 06:42:51 GMT") }, '$set': { updatedAt: new Date("Sun, 15 Sep 2019 06:42:51 GMT") }, 
'$pull': {  predictedEntityValues: { 'values.0.coords': {} } } }, {})

As you can see, it removes the $exists false in the parsed query: { predictedEntityValues: { 'values.0.coords': {} } } }

@vkarpov15 vkarpov15 added this to the 5.7.3 milestone Sep 23, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Sep 23, 2019
vkarpov15 added a commit that referenced this issue Sep 25, 2019
@vkarpov15
Copy link
Collaborator

Fix will be in 5.7.3. As a workaround, add coords to your schema. For example:

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh8166`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, { useNewUrlParser: true });
  await mongoose.connection.dropDatabase();

  const Model = mongoose.model('Test', Schema({
    name: String,
    predictedEntityValues: [{
      status: String,
      values: [{ text: String, coords: [Number] }]
    }]
  }));

  await Model.create({
    "_id" : new mongoose.Types.ObjectId("5d7ddedf92264a43ceee9f48"),
    "name" : "test123",
    "predictedEntityValues" : [
        {
            "status" : "DONE",
            "values" : [
                {
                    "text" : "123"
                }
            ]
        }
    ]
  });

  await Model.updateMany(
      {},
      {
        $pull: {
          predictedEntityValues: { 'values.0.coords': { $exists: false } },
        },
      }
    )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants