Mongodb 我是否正确地在mongoose中运行这个和查询,因为我没有得到任何东西

Mongodb 我是否正确地在mongoose中运行这个和查询,因为我没有得到任何东西,mongodb,express,mongoose,Mongodb,Express,Mongoose,这就是代码,我是否正确运行了和查询 const getUserMessages = async (id) => { const userMessages = await MessageCollection.find({}).and([{sentById: id}, {sentToId: id}]).sort({dateSent: 'ascending'}) return userMessages } 试试下面的方法 const userMessages = await Messa

这就是代码,我是否正确运行了
查询

const getUserMessages = async (id) => {
  const userMessages = await MessageCollection.find({}).and([{sentById: id}, {sentToId: id}]).sort({dateSent: 'ascending'})
  return userMessages
}
试试下面的方法

const userMessages = await MessageCollection.find({sentById: id, sentToId: id}).sort({dateSent: 'ascending'})

默认情况下,mongoose将对查询中的每个键应用AND条件,除非您使用
$或
或其他一些特殊查询参数进行了其他指定。上面的查询将找到您试图获取用户发送给自己的消息的所有消息,其中
sentById=sentToId

?(sendById=id和sendToId=id)?