猫鼬:查找具有特定字段的所有文档

吉贤门

我的一些文档中有一个这样的额外"Game"字段

{
    "_id" : NumberLong(204648),
    "loc" : [ 
        -41.2887953, 
        174.7772114
    ],
    "ts" : "2020-09-11T02:54:11Z",
    "v" : 52,
    "game": 2, <-------- 
}

我想返回这些在"Game"字段中具有任何值的文档

mongoose.connection.db.collection('games').find({ "game": {} }, (err, docs) => {
        console.log(docs);
    });

我试过了。但是,它只是返回undefined我如何只用console.log记录具有该"game"字段值的文档


const a = await mongoose.connection.db.collection('game').find({ game: { $exists: true } });
    console.log(a);

退货

Cursor {
  _readableState: ReadableState {
    objectMode: true,
    highWaterMark: 16,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: [],
    flowing: null,
    ended: false,
    endEmitted: false,
    reading: false,
    sync: true,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    destroyed: false,
    errored: null,
    closed: false,
    closeEmitted: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  },
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  operation: FindOperation {
    options: {
      skip: 0,
      limit: 0,
      raw: undefined,
      hint: null,
      timeout: undefined,
      slaveOk: true,
      readPreference: [ReadPreference],
      db: [Db],
      promiseLibrary: [Function: Promise]
    },
    ns: MongoDBNamespace { db: 'db', collection: 'game' },
    cmd: {
      find: 'db.game',
      limit: 0,
      skip: 0,
      query: [Object],
      raw: undefined,
      hint: null,
      timeout: undefined,
      slaveOk: true,
      readPreference: [ReadPreference]
    },
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined
    },
    cursorState: {
      cursorId: null,
      cmd: [Object],
      documents: [],
      cursorIndex: 0,
      dead: false,
      killed: false,
      init: false,
      notified: false,
      limit: 0,
      skip: 0,
      batchSize: 1000,
      currentLimit: 0,
      transforms: undefined,
      raw: undefined
    }
  },
  pool: null,
  server: null,
  disconnectHandler: undefined,
  bson: BSON {},
  ns: 'db.game',
  namespace: MongoDBNamespace { db: 'db', collection: 'game' },
  cmd: {
    find: 'db.game',
    limit: 0,
    skip: 0,
    query: { tg: [Object] },
    raw: undefined,
    hint: null,
    timeout: undefined,
    slaveOk: true,
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined
    }
  },
  options: {
    skip: 0,
    limit: 0,
    raw: undefined,
    hint: null,
    timeout: undefined,
    slaveOk: true,
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined
    },
    db: Db {
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      s: [Object],
      serverConfig: [Getter],
      bufferMaxEntries: [Getter],
      databaseName: [Getter],
      [Symbol(kCapture)]: false
    },
    promiseLibrary: [Function: Promise]
  },
  topology: NativeTopology {
    _events: [Object: null prototype] {
      authenticated: [Function (anonymous)],
      error: [Array],
      timeout: [Array],
      close: [Array],
      parseError: [Array],
      fullsetup: [Array],
      all: [Array],
      reconnect: [Array],
      commandStarted: [Function (anonymous)],
      commandSucceeded: [Function (anonymous)],
      commandFailed: [Function (anonymous)],
      serverOpening: [Function (anonymous)],
      serverClosed: [Function (anonymous)],
      serverDescriptionChanged: [Function (anonymous)],
      serverHeartbeatStarted: [Function (anonymous)],
      serverHeartbeatSucceeded: [Array],
      serverHeartbeatFailed: [Function (anonymous)],
      topologyOpening: [Function (anonymous)],
      topologyClosed: [Function (anonymous)],
      topologyDescriptionChanged: [Function (anonymous)],
      joined: [Array],
      left: [Function (anonymous)],
      ping: [Function (anonymous)],
      ha: [Function (anonymous)],
      connectionPoolCreated: [Function (anonymous)],
      connectionPoolClosed: [Function (anonymous)],
      connectionCreated: [Function (anonymous)],
      connectionReady: [Function (anonymous)],
      connectionClosed: [Function (anonymous)],
      connectionCheckOutStarted: [Function (anonymous)],
      connectionCheckOutFailed: [Function (anonymous)],
      connectionCheckedOut: [Function (anonymous)],
      connectionCheckedIn: [Function (anonymous)],
      connectionPoolCleared: [Function (anonymous)],
      open: [Function],
      reconnectFailed: [Function (anonymous)]
    },
    _eventsCount: 36,
    _maxListeners: Infinity,
    s: {
      id: 0,
      options: [Object],
      seedlist: [Array],
      state: 'connected',
      description: [TopologyDescription],
      serverSelectionTimeoutMS: 30000,
      heartbeatFrequencyMS: 10000,
      minHeartbeatFrequencyMS: 500,
      Cursor: [class Cursor extends CoreCursor],
      bson: BSON {},
      servers: [Map],
      sessionPool: [ServerSessionPool],
      sessions: Set(0) {},
      promiseLibrary: [Function: Promise],
      credentials: undefined,
      clusterTime: null,
      connectionTimers: Set(0) {},
      sCapabilities: [ServerCapabilities]
    },
    [Symbol(kCapture)]: false,
    [Symbol(waitQueue)]: Denque {
      _head: 2,
      _tail: 2,
      _capacity: undefined,
      _capacityMask: 3,
      _list: [Array]
    }
  },
  cursorState: {
    cursorId: null,
    cmd: {
      find: 'db.game',
      limit: 0,
      skip: 0,
      query: [Object],
      raw: undefined,
      hint: null,
      timeout: undefined,
      slaveOk: true,
      readPreference: [ReadPreference]
    },
    documents: [],
    cursorIndex: 0,
    dead: false,
    killed: false,
    init: false,
    notified: false,
    limit: 0,
    skip: 0,
    batchSize: 1000,
    currentLimit: 0,
    transforms: undefined,
    raw: undefined
  },
  logger: Logger { className: 'Cursor' },
  s: {
    numberOfRetries: 5,
    tailableRetryInterval: 500,
    currentNumberOfRetries: 5,
    state: 0,
    promiseLibrary: [Function: Promise],
    explicitlyIgnoreSession: false
  },
  [Symbol(kCapture)]: false
}

我没有得到任何文件console.log,只是这个Cursor返回。

JF

您可以通过$exists以下方式使用

db.collection.find({
  "game": {
    "$exists": true
  }
})

这里的例子

另外,另一个选择是比较不等于null:

db.collection.find({
  "game": {
    "$ne": null
  }
})

这里的例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

python:如何查找具有特定字段的文档

在mongodb中查找具有特定字段的所有集合

猫鼬对所有文档求和

如何使用猫鼬从集合的所有文档中删除指定字段?

猫鼬返回有关架构的所有字段

MongoDb:查找具有特定字段的所有集合中的所有(子)文档

猫鼬不会使用简单的find()返回文档中的所有字段

具有子文档属性的猫鼬组

查找具有特定数字字段mongodb的文档

猫鼬查找匹配输入子串的所有文档

具有许多文档和负载的猫鼬光标

猫鼬。在包含所有嵌套集合的集合中查找所有字段

猫鼬模式:使数组内的对象具有文档的唯一字段

猫鼬找到具有字段的用户?

检索猫鼬文档中对象的所有键

猫鼬-查找具有任何键和特定子键的对象

猫鼬:查找一组特定文章的所有标签

用猫鼬渲染所有文档的每个子文档

获取猫鼬类型的所有文档,但每个文档数组仅包含一个特定项

猫鼬:查找具有多个条目的混合模式类型文档

如何在Elasticsearch中查找具有特定字段的所有文档?

猫鼬删除不会删除所有文档

如何查找具有特定案头的所有文档

如何在字段中查找所有具有特定字符串的文档?(Elasticsearch)

如何在猫鼬中查找具有重复属性的文档?

对所有元素猫鼬的特定字段求和

更新猫鼬子文档数组中的所有项目

用猫鼬返回包含某个字段的所有文档

为什么猫鼬聚合方法不返回文档的所有字段?