Can't unset some values

Hi, I can’t remove some fields with a updateOne query or updateMany, these fields are not present anymore in the schema, but I still can’t remove them.
I’m using

osumieEnemyModel.updateMany({}, { $unset: { atk_mult: '',

            hp_mult: '',

            maxHp_mult: '',

            minHp_mult: '',

            maxAtk_mult: '',

            minAtk_mult: '',

    } })

I don’t use these fields anymore as they changed name.
EDIT: When I say I can’t remove them, running this query doesn’t change anything to the documents.

Hi @Simon_N_A,

You need a 1 instead of an empty string.

test:PRIMARY> db.coll.findOne()
{
	"_id" : ObjectId("6391ece76b130c4eb3fc8982"),
	"name" : "Max",
	"atk_mult" : "a",
	"hp_mult" : "b",
	"maxHp_mult" : "c",
	"minHp_mult" : "d",
	"maxAtk_mult" : "e",
	"minAtk_mult" : "f"
}
test:PRIMARY> db.coll.updateMany({},{$unset: {atk_mult: 1, hp_mult: 1, maxHp_mult: 1, minHp_mult: 1, maxAtk_mult: 1, minAtk_mult:1}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
test:PRIMARY> db.coll.findOne()
{ "_id" : ObjectId("6391ece76b130c4eb3fc8982"), "name" : "Max" }

Cheers,
Maxime.

Hi, thank you for replying, but changing these to 1 didn’t work. (I also saw on the docs that what we put after the fields in the $unset doesn’t change anything

The specified value in the $unset expression (i.e. "" ) does not impact the operation.

Here’s my schema:

new mongoose.Schema({

    name: { type: String, required: true },

    image: { type: String, required: true },

    min_hp: { type: Number, required: true },

    max_hp: { type: Number, required: true },

    atkMin_mult: {

        type: mongoose.Schema.Types.Decimal128, required: true, get: (v: any) => {

            const num = +v.toString()

            return num

        }

    },

    atkMax_mult: {

        type: mongoose.Schema.Types.Decimal128, required: true, get: (v: any) => {

            const num = +v.toString();

            return num;

        }

    },

    hpMin_mult: {

        type: mongoose.Schema.Types.Decimal128, required: true, get: (v: any) => {

            const num = +v.toString();

            return num;

        }

    },

    hpMax_mult: {

        type: mongoose.Schema.Types.Decimal128, required: true, get: (v: any) => {

            const num = +v.toString();

            return num;

        }

    },

    stun_chance: {

        type: mongoose.Schema.Types.Decimal128, default: 0.05, required: true, get: (v: any) => {

            const num = +v.toString();

            return num

        }

    }

})

(I needed to .toString() because I was getting a Decimal128 Object, when I only wanted the value, if you have a cleaner way to do that, I’d be thankful too)

Hi @Simon_N_A,

  • Which version of MongoDB are you running?
  • Where you able to reproduce my example from my previous post?
  • If this works, what’s the difference with your data set then ?
  • How is this related to Node.js? It’s a pure MongoDB command here, no?

What’s the error message? What do you get? Can you reproduce in a small script like me?

Oops my bad! :face_exhaling:
You are totally right but this was the only suspicious thing to me that I could find in your question and I didn’t think of it twice as I made it work in my little script.

No idea for this one, I’m not the best in JS. :sweat_smile:
I stopped at “Java”. I couldn’t handle the “script”. :wink:

Cheers,
Maxime.

I dont get any error, the data is just not modified.
Is there an option that would prevent me from doing operations on fields that are not present in the schema ?

Hi again, apparently your solution worked when using it in mongosh, in compass.
But, how is it possible that the same query didn’t work using the mongoose library ?
I’m on MongoDB 5.0.8 Community

No idea, I never used Mongoose and I generally don’t really recommend using an extra layer between the back-end code and the JS Driver.

My guess is that something could be wrong in your schema or the query isn’t translated correctly and thus isn’t doing what you think it’s doing when it’s executed.