[英]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.