使用区分符更新mongodb文档

时间:2019-08-12 14:09:00

标签: mongodb express mongoose

我有一个关于歧视者和mongoose / mongodb的问题。我想了解如何更新一个包含2个属性的文档,这些属性是标识符数组。

这是我的猫鼬模特:

const mongoose = require('mongoose');
const carSchema = require('./car');
const continentalWheelSchema = require('./wheel/continental');
const michelinWheelSchema = require('./wheel/michelin');

const CarSchema = new mongoose.Schema({}, { discriminatorKey: 'type', _id: false } );
const WheelSchema = new mongoose.Schema({}, { discriminatorKey: 'type', _id: false } );

const ClientParkSchema = new mongoose.Schema({
    customer: { type: String, required: true },
    created_at: { type: Date, default: Date.now },
    updated_at: { type: Date, default: Date.now },
    cars: [CarSchema],
    wheels: [WheelSchema]
});

ClientParkSchema.path('cars').discriminator('break', carSchema);
ClientParkSchema.path('wheels').discriminator('continental', continentalWheelSchema);
ClientParkSchema.path('wheels').discriminator('michelin', michelinWheelSchema);

module.exports = {
    ClientPark: mongoose.model('client_park', ClientParkSchema, 'client_park')
};

/////////////////////// CAR SCHEMA ////////////////////////

const mongoose = require('mongoose');

const CarSchema = new mongoose.Schema({
    brand: { type: String, required: true },
    model: { type: String, required: true },
}, { _id: false } );

module.exports = CarSchema;

/////////////////////// CONTINENTAL WHEEL SCHEMA ////////////////////////

const mongoose = require('mongoose');

const ContinentalWheelSchema = new mongoose.Schema({
    model: { type: String, required: true }
}, { _id: false } );

module.exports = ContinentalWheelSchema;

/////////////////////// MICHELIN WHEEL SCHEMA ////////////////////////

const mongoose = require('mongoose');

const MichelinWheelSchema = new mongoose.Schema({
    price: { type: number, required: true},
    model: { type: String, required: true },
    season: { type: String, required: true }
}, { _id: false } );

module.exports = MichelinWheelSchema;

mongodb查询:

db.client_park.updateOne({_id: ObjectId('5d3865a4e56473518adabd35'), "car.type": 'break',"wheel.type": "michelin"}, {$set: {"car.$.brand": 'Ford',"wheel.$.price": 100,"wheel.$.season": "summer"}})

该查询似乎有效,但是文档未更新。因此,我想知道如何通过区分符(如果存在)来更新我的文档。

非常感谢您!

编辑:

我要更新的文档:

{  
   "_id":"5d39a19e2376c7089ec7707b",
   "customer":"5d0757aa4b6620003335aff2",
   "cars":[  
      {  
         "type":"break",
         "brand":"Ford"
      }
   ],
   "created_at":"2019-07-25T12:33:34.799Z",
   "updated_at":"2019-07-25T12:33:34.799Z",
   "_v":0
}

0 个答案:

没有答案