如果我有一個帶有地圖的貓鼬模式。是否可以僅通過鍵查詢地圖中的元素
const userSchema = new Schema( {
socialHandles: {
type: Map,
of: String
},
active: {type:Boolean, default:true}
} );
我能夠查詢并找到一個用戶,其社交句柄具有 instagram 鍵,并且 instagram 鍵使用以下語法保存特定值。
let user = await User.findOne({ 'socialHandles.instagram': 'jondoe' });
如果存在名為 instagram 的鍵,我正在尋找一種查詢和查找用戶的 instagram id 的方法。以下形式的某些內容,即使用社交句柄 instagram 獲取用戶(稍后我將需要 instagram 的值)。
let user = await User.findOne({ 'socialHandles.instagram': * }); // this is just a wrong syntax to explain what I want to achieve
uj5u.com熱心網友回復:
用 exists
let user = await User.findOne().exists('socialHandles.instagram')
uj5u.com熱心網友回復:
你可以這樣使用$exists:
User.findOne({
"socialHandles.instagram": {
"$exists": true
}
})
示例在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339801.html
標籤:javascript 节点.js 猫鼬 猫鼬模式
