Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongoose:查找所有具有特定字段的文档_Node.js_Mongoose - Fatal编程技术网

Node.js mongoose:查找所有具有特定字段的文档

Node.js mongoose:查找所有具有特定字段的文档,node.js,mongoose,Node.js,Mongoose,我的一些文档有一个额外的“Game”字段,如下所示 { "_id" : NumberLong(204648), "loc" : [ -41.2887953, 174.7772114 ], "ts" : "2020-09-11T02:54:11Z", "v" : 52, "game": 2, &l

我的一些文档有一个额外的
“Game”
字段,如下所示

{
    "_id" : NumberLong(204648),
    "loc" : [ 
        -41.2887953, 
        174.7772114
    ],
    "ts" : "2020-09-11T02:54:11Z",
    "v" : 52,
    "game": 2, <-------- 
}
我试过了。但是,它只返回
未定义的
。如何仅记录具有
“game”
字段值的文档


返回

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
}
我没有得到任何document console.log,但只有此光标返回。

您可以这样使用:

db.collection.find({
“游戏”:{
“$exists”:true
}
})
范例

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

db.collection.find({
“游戏”:{
“$ne”:空
}
})

示例

如何返回文档值而不是光标?是否使用.find({})?我添加了我的查询结果。我只是看到光标返回。我想问题是你必须使用
wait
或回调,而不是两者都使用。
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
}