繁体   English   中英

从数组中删除不起作用 - Mongodb

[英]Removing from array is not working - Mongodb

我有一个如下所示的文档:

email:"y@y.com"
playlist:"name1"
IDs:
  0:"5db87297b96b1405403bcf13"
  1:"5dc88666f5ba4a2901062398"
  2:"5dc88666f5ba4a2901062398"
  3:"5dc88666f5ba4a2901062398"

要从数组中删除的 mongo 查询:

    let removingItem = {$pull:{'IDs':'5db87297b96b1405403bcf13'}}
    collection.updateOne({'email':'y@y.com','playlistName':'name1'},removingItem,function(err,data){
        //res.send({'message':'removed'})
        console.log(data)
        client.close();
    })

我不知道为什么这不起作用。 我也尝试过平等:

let removingItem = {$pull:{'IDs':{$eq:'5db87297b96b1405403bcf13'}}}

对于这两种方法,我在控制台上得到的输出是:

CommandResult {
  result: { n: 1, nModified: 1, ok: 1 },
  connection: Connection {
    _events: [Object: null prototype] {
      error: [Function],
      close: [Function],
      timeout: [Function],
      parseError: [Function],
      message: [Function]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    id: 0,
    options: {
      host: 'localhost',
      port: 27017,
      size: 5,
      minSize: 0,
      connectionTimeout: 30000,
      socketTimeout: 360000,
      keepAlive: true,
      keepAliveInitialDelay: 300000,
      noDelay: true,
      ssl: false,
      checkServerIdentity: true,
      ca: null,
      crl: null,
      cert: null,
      key: null,
      passphrase: null,
      rejectUnauthorized: false,
      promoteLongs: true,
      promoteValues: true,
      promoteBuffers: false,
      reconnect: true,
      reconnectInterval: 1000,
      reconnectTries: 30,
      domainsEnabled: false,
      legacyCompatMode: true,
      disconnectHandler: [Store],
      cursorFactory: [Function: Cursor],
      emitError: true,
      monitorCommands: false,
      promiseLibrary: [Function: Promise],
      clientInfo: [Object],
      servers: [Array],
      caseTranslate: true,
      dbName: 'test',
      socketTimeoutMS: 360000,
      connectTimeoutMS: 30000,
      retryWrites: true,
      useRecoveryToken: true,
      readPreference: [ReadPreference],
      bson: BSON {}
    },
    logger: Logger { className: 'Connection' },
    bson: BSON {},
    tag: undefined,
    maxBsonMessageSize: 67108864,
    port: 27017,
    host: 'localhost',
    socketTimeout: 360000,
    keepAlive: true,
    keepAliveInitialDelay: 300000,
    connectionTimeout: 30000,
    responseOptions: { promoteLongs: true, promoteValues: true, promoteBuffers: false },
    flushing: false,
    queue: [],
    writeStream: null,
    destroyed: false,
    hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c',
    workItems: [],
    socket: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'localhost',
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      timeout: 360000,
      [Symbol(asyncId)]: 35,
      [Symbol(kHandle)]: [TCP],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 360000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 11117,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 44,
        [Symbol(triggerId)]: 35
      },
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0
    },
    buffer: null,
    sizeOfMessage: 0,
    bytesRead: 0,
    stubBuffer: null,
    ismaster: {
      ismaster: true,
      maxBsonObjectSize: 16777216,
      maxMessageSizeBytes: 48000000,
      maxWriteBatchSize: 100000,
      localTime: 2019-12-14T19:20:25.165Z,
      logicalSessionTimeoutMinutes: 30,
      minWireVersion: 0,
      maxWireVersion: 7,
      readOnly: false,
      ok: 1
    },
    lastIsMasterMS: 4
  },
  message: BinMsg {
    parsed: true,
    raw: <Buffer 3c 00 00 00 7d 45 00 00 01 00 00 00 dd 07 00 00 00 00 00 00 00 27 00 00 00 10 6e 00 01 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 01 00 00 00 01 6f 6b ... 10 more bytes>,
    data: <Buffer 00 00 00 00 00 27 00 00 00 10 6e 00 01 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 01 00 00 00 01 6f 6b 00 00 00 00 00 00 00 f0 3f 00>,
    bson: BSON {},
    opts: { promoteLongs: true, promoteValues: true, promoteBuffers: false },
    length: 60,
    requestId: 17789,
    responseTo: 1,
    opCode: 2013,
    fromCompressed: undefined,
    responseFlags: 0,
    checksumPresent: false,
    moreToCome: false,
    exhaustAllowed: false,
    promoteLongs: true,
    promoteValues: true,
    promoteBuffers: false,
    documents: [ [Object] ],
    index: 44,
    hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c'
  },
  modifiedCount: 1,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

不幸的是,我无法更改数组的格式,否则我将不得不更改很多依赖于这个简单结构的东西。

预先感谢您的帮助

以下是$pull如何处理您正在使用的数据。 注意IDs字段是一个嵌入文档的数组。 这个例子:

输入文件:

{
        "_id" : 1,
        "ids" : [
                {
                        "0" : "zero"
                },
                {
                        "1" : "one"
                },
                {
                        "2" : "two"
                }
        ]
}

更新查询:

db.test.updateOne( { _id: 1 }, { $pull: { "ids": { "1": "one" } } } )

结果:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

{
        "_id" : 1,
        "ids" : [
                {
                        "0" : "zero"
                },
                {
                        "2" : "two"
                }
        ]
}



更新:

使用输入文档:

{ "_id" : 1, "ids" : [ "zero", "one", "two" ] }

并且,更新操作:

db.test.updateOne( { _id: 1 }, { $pull: { ids : "one" } } )

结果将是:

{ "_id" : 1, "ids" : [ "zero", "two" ] }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM