2017-06-27 10 views
0

私の文書には小文字のフィールドをいくつか保存したい。したがって、私は値を挿入するsave()を使用していたとき、私はfindOneAndUpdate()の実行中にMongooseの小文字のフラグが無視される

let body = new Schema({ 
    id: { type: Number, unique: true }, 
    group_name: { type: String, lowercase: true }, 
    type_name: { type: String, lowercase: true }, 
    full_spectral_class: { type: String, lowercase: true }, 
    spectral_class: { type: String, lowercase: true }, 
    spectral_sub_class: { type: String, lowercase: true }, 
    luminosity_class: { type: String, lowercase: true }, 
    luminosity_sub_class: { type: String, lowercase: true } 
}) 

のようなスキーマを作成し、すべての値は小文字の文字列として保存されてきています。しかし、私がfindOneAndUpdate()を使って更新しようとしたとき、その値はそのままです。私はむしろ、同じスタイルに従ったかなりの数のフィールドとスキーマがあるので、すべてのフィールドを手動で小文字に変換しません。

+2

あなたはおそらく、[クエリを実行するときに 'true'に' runSettersOnQuery']を設定する必要があります(http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate)。 – str

+0

それはちょっと働いた。 'schema.findOneAndUpdate(query、config)'で 'runSettersOnQuery:true'を使用しても動作しませんでした。スキーマを[ここ](http://mongoosejs.com/docs/queries.html)として定義しながら、オプションとして使用しなければなりませんでした。 –

答えて

2

あなたはrunSettersOnQuerytrueに設定する必要があります。

const options = { upsert: true, runSettersOnQuery : true }; 
await yourModel.findOneAndUpdate(searchCriteria, newSetOfValues, options); 

は、インターネット上のいくつかのサンプルがありますが、多分あなたはmongoose docsから、この一つの有用な見つけることができます。

関連する問題