【问题标题】:mongo db + node returns the connection stringmongodb + node 返回连接字符串
【发布时间】:2021-08-17 22:05:45
【问题描述】:

我正在开发一个抓取所有电报消息的程序,但我们发现我们需要一种简单的方法来插入和读取数据(crud)。我选择函数式编程是因为 oop 对我来说太模仿和太复杂(如果有人有建议联系我,因为我不知道从哪里开始)。因为我想要像value.insert 这样的东西,但经过一些研究并询问cs老师后,我发现这行不通。

查找数据的函数:

   connect.then( async () => {
    const dbo = client.db('telegram');
    const options = {
      projection: {_id : 1, name: 1, town: 1},
    }
   const result = await dbo.collection(collection).find(data)
    console.log(result)
  })
}

// how the function is called
Find('dev',{name: 'Red'})

下面的代码块是如何 console.log(result);看起来像

  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false,
  [Symbol(topology)]: Topology {
    _events: [Object: null prototype] {
      topologyDescriptionChanged: [Array],
      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)],
      commandStarted: [Function (anonymous)],
      commandSucceeded: [Function (anonymous)],
      commandFailed: [Function (anonymous)],
      serverOpening: [Function (anonymous)],
      serverClosed: [Function (anonymous)],
      serverDescriptionChanged: [Function (anonymous)],
      topologyOpening: [Function (anonymous)],
      topologyClosed: [Function (anonymous)],
      error: [Function (anonymous)],
      timeout: [Function (anonymous)],
      close: [Function (anonymous)],
      serverHeartbeatStarted: [Function (anonymous)],
      serverHeartbeatSucceeded: [Function (anonymous)],
      serverHeartbeatFailed: [Function (anonymous)]
    },
    _eventsCount: 25,
    _maxListeners: undefined,
    bson: [Object: null prototype] {
      serialize: [Function: serialize],
      deserialize: [Function: deserialize]
    },
    s: {
      id: 0,
      options: [Object: null prototype],
      seedlist: [Array],
      state: 'connected',
      description: [TopologyDescription],
      serverSelectionTimeoutMS: 30000,
      heartbeatFrequencyMS: 10000,
      minHeartbeatFrequencyMS: 500,
      servers: [Map],
      sessionPool: [ServerSessionPool],
      sessions: Set(0) {},
      credentials: [MongoCredentials],
      clusterTime: [Object],
      connectionTimers: Set(0) {},
      detectShardedTopology: [Function: detectShardedTopology],
      detectSrvRecords: [Function: detectSrvRecords],
      srvPoller: [SrvPoller]
    },
    [Symbol(kCapture)]: false,
    [Symbol(waitQueue)]: Denque {
      _head: 3,
      _tail: 3,
      _capacity: undefined,
      _capacityMask: 3,
      _list: [Array]
    }
  },
  [Symbol(namespace)]: MongoDBNamespace { db: 'telegram', collection: 'dev' },
  [Symbol(documents)]: [],
  [Symbol(initialized)]: false,
  [Symbol(closed)]: false,
  [Symbol(killed)]: false,
  [Symbol(options)]: {
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined,
      maxStalenessSeconds: undefined,
      minWireVersion: undefined
    },
    fieldsAsRaw: {},
    promoteValues: true,
    promoteBuffers: false,
    promoteLongs: true,
    serializeFunctions: false,
    ignoreUndefined: false,
    bsonRegExp: false,
    raw: false
  },
  [Symbol(filter)]: { name: 'Red' },
  [Symbol(builtOptions)]: {
    raw: false,
    promoteLongs: true,
    promoteValues: true,
    promoteBuffers: false,
    ignoreUndefined: false,
    bsonRegExp: false,
    serializeFunctions: false,
    fieldsAsRaw: {},
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined,
      maxStalenessSeconds: undefined,
      minWireVersion: undefined
    }
  }
}

预期的行为应该是它返回所有具有值名称的文档:红色

如果有帮助,这就是数据库的外观:

[对于最小文本]我尝试在网上查找是否有关于返回连接字符串的模块的一些内容

【问题讨论】:

    标签: javascript node.js mongodb find


    【解决方案1】:

    如果你想从没有对象的模型中获取数据 你可以使用

    exports.findFriend =  async (userName,user)=>{
        return await User.findOne({_id:user._id},{userName = 'Red');
    };
    

    如果要从具有对象的模型中获取数据,可以使用

    exports.findFriend =  async (userName,user)=>{
        return await User.findOne({_id:user._id},{friends:{$elemMatch:{userName:userName}}});
    };
    

    【讨论】:

    • 是的,谢谢,但我需要一种方法来查找多个文档
    • 用于多个文档。使用:requestCondition 将根据您的意愿。 let result = await User.find(requestCondition) .sort( { _id : -1 } ) .skip(pg.skip) .limit(pg.pageSize) .exec();
    【解决方案2】:

    原来我忘了对结果使用 .toarray()

    【讨论】:

      猜你喜欢
      • 2011-09-05
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多