The schema configuration item shardKey in mongoose does not work. It is always reminded to bring the chip key.

problem description

official documentation shardKey configuration instructions:

configure the code according to the official document as follows: (the code has been deleted)

 new Schema( {
        eId: { type: String, default: null },
        eArticleId: { type: NumberLong },
        title: { type: String },
        type: { type: String, default: "article" },
        author: { type: String, default: null },
       
    }, {
        shardKey: {shardKey: {eArticleId: 1}}, // eArticleId
        versionKey: false
    });
    
    const name = "article";

    let Article = mongoConn.model(name, userSchema, name);
    async function updateDoc(){
        let doc = await Article.findOneAndUpdate({_id: "5a53e3a98fc5e3000143f500"}, {$set: {level: 1}}).exec()
        console.log("doc = ", doc);
    }

get the error message directly: MongoError: query for sharded findAndModify must have shardkey .
I know that you can update successfully with the slice key, but according to the official document, as long as you configure the shardKey item of schema, you can do without the slice key for the update operation. I wonder if I understand it correctly? But I repeatedly test, do not take the key to update always report the above errors, I do not know if there is a good way to deal with it?

Dec.17,2021

are you writing this place wrong? shardKey: {shardKey: {eArticleId: 1} should be shardKey: {eArticleId: 1} .

Menu