猫鼬findOneandUpdate不会将所有字段都添加到文档中

时间:2020-11-08 18:41:04

标签: node.js mongoose

好,所以我为此花费了数小时,并且进行了各种Google搜索,但似乎无法解决这个问题。所以,我在这里寻求帮助...

我正在尝试使用mongoose.findOneAndUpdate()来添加文档或将现有文档更新到集合中。在成功之前,我已经做过很多次了,但是现在我很沮丧。

在mongodb中创建文档时,它仅包含以下内容:

{
  _id: <some mongo id>
  faFlightID: 4839-fjgnkbk-adhoc
  positions: [Array of Objects]  <----These appear to be correct.
}

就是这样。它缺少所有其他字段。我觉得我似乎完全看不见任何东西,但是在这一点上,我一直盯着它看了很长时间,我可能看不到树木茂盛的森林

这是我的猫鼬代码:

const Flights = require('../models/faFlights.model');

const saveFlight = async (flight) => {
    let position = {
        timestamp: flight.timestamp,
        longitude: flight.longitude,
        latitude: flight.latitude,
        groundspeed: flight.groundspeed,
        altitude: flight.altitude,
        heading: flight.heading,
        altitudeStatus: flight.altitudeStatus,
        altitudeChange: flight.altitudeChange,
    };
    
    const filter = { faFlightID: flight.faFlightID };
    const update = { flight, $push: { positions: position } };

    try {
        let result = await Flights.findOneAndUpdate(filter, update {
            upsert: true,
            new: true,
        });
        console.log(result);
    } catch (error) {
        console.log(error);
    }
};

这是我的faflights.model

const mongoose = require('mongoose');

const positionSchema = new mongoose.Schema({
    timestamp: {
        type: Date,
        set: (d) => formatEpoch(d),
    },
    longitude: Number,
    latitude: Number,
    groundspeed: Number,
    altitude: Number,
    heading: Number,
    altitudeStatus: String,
    altitudeChange: String,
});

const faflightSchema = new mongoose.Schema(
    {
        TALON_ACT_ID: String,
        faFlightID: String,
        ident: {
            type: String,
            set: (acreg) => modifyACRegistration(acreg),
        },
        prefix: String,
        type: String,
        suffix: String,
        origin: String,
        destination: String,
        timeout: Number,
        departureTime: {
            type: Date,
            set: (d) => formatEpoch(d),
        },

        firstPositionTime: {
            type: Date,
            set: (d) => formatEpoch(d),
        },
        arrivalTime: {
            type: Date,
            set: (d) => formatEpoch(d),
        },

        positions: [positionSchema],
        lowLongitude: Number,
        lowLatitude: Number,
        highLongitude: Number,
        highLatitude: Number,
        updateType: String,
        waypoints: String,
    },
    { collection: 'faflights' }
);

//Convert AC Registration formatting
const modifyACRegistration = (reg) => {
    let firstCharacter = reg.substring(0, 1);
    let remainingCharacter = reg.substring(1, 5);
    return `${firstCharacter}-${remainingCharacter}`;
};

const formatEpoch = (epoch) => {
    if (!epoch) return;
    return new Date(epoch * 1000);
};

module.exports = mongoose.model('faflights', faflightSchema);

我全神贯注。

1 个答案:

答案 0 :(得分:0)

显然,我只需要离开我的工作站一会儿,或者再喝些咖啡……或两者兼而有之。需要使用$ set使它变得更好。

    const filter = { faFlightID: flight.faFlightID };
    const update = { $set: flight, $push: { positions: position } };

    try {
        let result = await Flights.findOneAndUpdate(filter, update {
            upsert: true,
            new: true,
        });
        console.log(result);
    } catch (error) {
        console.log(error);
    }