【问题标题】:Mongodb - Unable to query multiple documents in Realm TriggerMongodb - 无法在领域触发器中查询多个文档
【发布时间】:2023-03-24 16:00:01
【问题描述】:

我正在使用 Real Scheduled Triggers 来运行一些数据验证的后台作业。 在 Trigger 函数中,我想查询集合中的所有文档,但这不起作用。但是当我尝试 findOne 文档时,我得到了响应。

//不起作用,返回空白

const collection =  context.services.get("Cluster0").db("testdocument").collection("testcollection");
return collection.find({}).then((result)=>{  console.log('data : ',JSON.stringify(result));     })

//有效,返回结果

const collection =  context.services.get("Cluster0").db("testdocument").collection("testcollection");

return collection.find({isActive:'true'}).then((result)=>{ console.log('data :',JSON.stringify(result)); })

如果我在这里做错了什么,任何人都可以提出建议。

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    我认为您的问题对 //doesn't work 和 //works 都使用了相同的代码。但我认为 findOne 是有效的,因为它返回一个文档,而 find 返回一个游标。如文档中所示,您必须在 .then() 之前使用 .toArray() 但无论如何都不明显。

    https://docs.mongodb.com/realm/mongodb/actions/collection.find/

    【讨论】:

    • 抱歉我的不好,更新了sn-p。当然让我试试
    • 你确定这行得通吗:return collection.find({isActive:'true'}).then((result)=>{ console.log('data :',JSON.stringify(result )); })
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多