如何使用猫鼬将特定文档从一个集合转移到另一个集合?

时间:2019-11-07 09:30:53

标签: node.js mongodb mongoose mongoose-schema

我有两个模型-LifeMembers和AnnualMembers。我想将特定文档从AnnualMembers转移到LifeMembers。因此,我使用findById从AnnualMembers中查询文档,然后将其保存到LifeMembers。但是在尝试将其保存到LifeMembers时,我不断收到DocumentNotFoundError。请帮忙。

这是代码:

AnnualMembers.findById(id).then(member=>{
        let life = new LifeMembers(member);
        life.save().then(member=>res.json(member));
                   .catch(err=>console.log(err));
     }).catch(err=>console.log(err));

这是我不断得到的错误:

{ DocumentNotFoundError: No document found for query "{ _id: 5dc194b3fd0be643f45c4edb }" on model "LifeMember"
    at new DocumentNotFoundError (E:\Projects\orsi\node_modules\mongoose\lib\error\notFound.js:34:11)
    at $__handleSave (E:\Projects\orsi\node_modules\mongoose\lib\model.js:371:17)
    at E:\Projects\orsi\node_modules\mongoose\lib\model.js:303:9
    at result (E:\Projects\orsi\node_modules\mongodb\lib\operations\execute_operation.js:75:17)
    at session.endSession (E:\Projects\orsi\node_modules\mongodb\lib\operations\execute_operation.js:64:11)
    at ClientSession.endSession (E:\Projects\orsi\node_modules\mongodb\lib\core\sessions.js:135:41)
    at executeCallback (E:\Projects\orsi\node_modules\mongodb\lib\operations\execute_operation.js:59:17)
    at updateCallback (E:\Projects\orsi\node_modules\mongodb\lib\operations\update_one.js:41:3)
    at updateDocuments (E:\Projects\orsi\node_modules\mongodb\lib\operations\update_one.js:24:64)
    at handleCallback (E:\Projects\orsi\node_modules\mongodb\lib\utils.js:129:55)
    at coll.s.topology.update (E:\Projects\orsi\node_modules\mongodb\lib\operations\common_functions.js:375:5)
    at handler (E:\Projects\orsi\node_modules\mongodb\lib\core\sdam\topology.js:1000:24)
    at wireProtocol.(anonymous function) (E:\Projects\orsi\node_modules\mongodb\lib\core\sdam\server.js:457:5)
    at E:\Projects\orsi\node_modules\mongodb\lib\core\connection\pool.js:408:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  message:
   'No document found for query "{ _id: 5dc194b3fd0be643f45c4edb }" on model "LifeMember"',
  name: 'DocumentNotFoundError',
  result:
   CommandResult {
     result:
      { n: 0,
        nModified: 0,
        opTime: [Object],
        electionId: 7fffffff0000000000000004,
        ok: 1,
        operationTime: [Timestamp],
        '$clusterTime': [Object] },
     connection:
      Connection {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        id: 1,
        options: [Object],
        logger: [Logger],
        bson: BSON {},
        tag: undefined,
        maxBsonMessageSize: 67108864,
        port: 27017,
        host: 'cluster0-shard-00-01-pcn5n.gcp.mongodb.net',
        socketTimeout: 360000,
        keepAlive: true,
        keepAliveInitialDelay: 300000,
        connectionTimeout: 30000,
        responseOptions: [Object],
        flushing: false,
        queue: [],
        writeStream: null,
        destroyed: false,
        hashedName: '2d8e7dfad852d7e83e92f54b67929766ab731839',
        workItems: [],
        socket: [TLSSocket],
        buffer: null,
        sizeOfMessage: 0,
        bytesRead: 0,
        stubBuffer: null,
        ismaster: [Object],
        lastIsMasterMS: 801 },
     message:
      BinMsg {
        parsed: true,
        raw:
         <Buffer f5 00 00 00 52 ed 45 01 0b 00 00 00 dd 07 00 00 00 00 00 00 00 e0 00 00 00 10 6e 00 00 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 03 6f 70 ... >,
        data:
         <Buffer 00 00 00 00 00 e0 00 00 00 10 6e 00 00 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 03 6f 70 54 69 6d 65 00 1c 00 00 00 11 74 73 00 01 00 00 ... >,
        bson: BSON {},
        opts: [Object],
        length: 245,
        requestId: 21359954,
        responseTo: 11,
        opCode: 2013,
        fromCompressed: undefined,
        responseFlags: 0,
        checksumPresent: false,
        moreToCome: false,
        exhaustAllowed: false,
        promoteLongs: true,
        promoteValues: true,
        promoteBuffers: false,
        documents: [Array],
        index: 229,
        hashedName: '2d8e7dfad852d7e83e92f54b67929766ab731839' },
     modifiedCount: 0,
     upsertedId: null,
     upsertedCount: 0,
     matchedCount: 0,
     '$where': { _id: 5dc194b3fd0be643f45c4edb } },
  numAffected: 0,
  filter: { _id: 5dc194b3fd0be643f45c4edb },
  query: { _id: 5dc194b3fd0be643f45c4edb } }

1 个答案:

答案 0 :(得分:0)

问题解决了。在使用findById()查询时,使用了lean()方法: AnnualMembers.findById(id).lean().then(member=>{/*...*/})