繁体   English   中英

如何在 mongodb nodejs 中查找数组

[英]how to find array in mongodb nodejs

bookHistory:(memberId)=>{

        return new Promise(async(resolve,reject)=>{
           const historyData=await db.get().collection('bookborrow').find({userId:(memberId)}).toArray()
            const bookid=  await historyData.map(element => {
                 return element.data.bookname
             }) 
              console.log(bookid)
              const getBookName=await db.get().collection('bookdetails').find({_id:{$in:[objectid[bookid]]}}).toArray()
              console.log(getBookName)
     })
   } 

historyData 是 output 看起来像 ['62e53c1ff5d4c45fb7853d9e','62ea6268fd2fe8616cd4c17b'] 我需要数据从另一个集合中获取这个 _id 数据

我想您打算使用 ids 数组进行搜索。 如果是这样,这应该工作。

 import {Types} from 'mongoose'; const historyData=await db.get().collection('bookborrow').find({userId:(memberId)}).toArray() const bookIds = await historyData.map(element => { return new Types.ObjectId(element.data.bookname) }) const getBookName = await db.get().collection('bookdetails').find({ _id: { $in: bookIds }, }).toArray()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM