0

Given the following scenario Schema, Dasbhoards are being actually added to set correctly for upsert. Im trying to $pullAll with no sucess at all.

/**
 * Dashboard Schema
 */
const DashboardSchema = new mongoose.Schema({
  _user: { type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    unique: false,
  },
  _cards: [{
    _id: false,
    cardId: { type: Schema.Types.ObjectId },
    cardType: {
      type: String,
      enum: ['BATTLE', 'QUEST', 'MISSION'],
    },
  }],
});

And Statics

/**
* DASHBOARD_STATICS
**/
pullAndUpdate(dashboardId, cards) {
return this.findOneAndUpdate(
  {
    _id: dashboardId,
  },
  {
    $pullAll: { _cards: [cards] },
  },
  {
    upsert: false,
    setDefaultsOnInsert: false,
    new: true,
  },
  )
.exec()
  .then((Dashboard) => {
    if (Dashboard) {
      return Dashboard;
    }
    const err = new Error('Error Upserting Dashboard', 
    httpStatus.NOT_FOUND);
    return Promise.reject(err);
    });
  },
};

` Where cards param in pullAndUpdate func is getting an array of objects like

{
  "cardId":ObjectID("41224d776a326fb40f000009")
}

This is how mongo record looks like

{
    "_id" : ObjectId("5ae3a4d6186af4a878dda65d"),
    "_user" : ObjectId("41224d776a326fb40f000001"),
    "__v" : 0,
    "_cards" : [ 
        [ 
            {
                "cardType" : "MISSION",
                "cardId" : ObjectId("41224d776a326fb40f000007")
            }
        ], 
        [ 
            {
                "cardType" : "MISSION",
                "cardId" : ObjectId("41224d776a326fb40f000009")
            }
        ]
    ]
}

Any thoughts ?

user2821789
  • 11
  • 1
  • 2
  • Why not just use `$pull` i.e `{ $pull: { _cards: cards } }` since it's already just a document. If you wanted a list then you can use `$in` i.e `{ $pull: { _cards: { cardId: { $in: [ObjectId("41224d776a326fb40f000007"), ObjectId("41224d776a326fb40f000009")] } } } }`. The `$pullAll` operator is for a "list of values" **only** when you read the documentation. None of your "objects" are an "exact match" for the provided detail, and therefore it fails to update. – Neil Lunn Apr 28 '18 at 11:38
  • Sound like a great explanation to me, thanks – user2821789 Apr 28 '18 at 12:10

0 Answers0