установить пустые значения в MongoDB - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь установить некоторые значения для устройств, у которых нет этих параметров, но для dataPersist, и каждая из временных меток не работает. Я не знаю, проблема ли это в структуре if, которую я использую, но для dataContainer и dataImageList работает.

 public getDeviceById = (deviceId: string): Promise<IDevice> => {
        return new Promise((resolve, reject) => {
            Device.findOne({ _id: mongoose.Types.ObjectId(deviceId) }).then((doc: any) => {
                if (doc) {

                    if  (!doc.dataPersist) {
                        Device.updateOne({_id: mongoose.Types.ObjectId(deviceId) }, { $set: { dataPersist: { persistSize: "", timestamp: new Date()} }})
                    }
                    if (!doc.dataContainer) {

                        Device.updateOne({_id: mongoose.Types.ObjectId(deviceId) }, { $set: { dataContainer: { containerInfo:[], dockerInfo: [], timestamp: new Date()} }})
                    }

                    if(!doc.dataImageList) {

                        Device.updateOne({_id: mongoose.Types.ObjectId(deviceId) }, { $set: { dataImageList: { imageList:[], timestamp: new Date()} }})
                    }

                        resolve(doc);


                } else {
                    reject("fieldNotFoundError,device,getDeviceById");
                }
            }).catch((err: any) => {
                console.error(err);
                reject("databaseError,043");
            });
        });
    }

Почему?

Это моя модель:

import mongoose from "mongoose";
import { IDevice } from "./iDevice";

interface IDeviceModel extends IDevice, mongoose.Document {
}

const deviceSchema = new mongoose.Schema({
    dataPersist: {
        persistSize: String,
        timestamp: Date
    },
    dataImageList: {
        imageList: Array,
        timestamp: Date
    },
    dataContainer: {
        containerInfo: Array,
        dockerInfo: Array,
        timestamp: Date
    }
});

const Device = mongoose.model<IDeviceModel>("device", deviceSchema);

export = Device;

Заранее благодарю за вашу помощь.

...