关于mongodb 数组内嵌对象,对象里嵌对象。查询的问题
[
{
playerId:'111',
playerName:'昵称2',
channels:[
{
channel:{
channelId:1,
channelName:'test'
}
},
{
channel:{
channelId:2,
channelName:'test2'
}
}
]
},
{
playerId:'222',
playerName:'昵称2',
channels:[
{
channel:{
channelId:3,
channelName:'test3'
}
},
{
channel:{
channelId:4,
channelName:'test4'
}
}
]
}
]
如上结构:根据channelId 查询当前Player对象,试用了$elemMatch 但是我这数组里嵌了2层对象,行不通。有人解答下么。
1 回复
问题已解决了。
db.players.find({"$where":function(){
var channels = this.channels;
for(var i=0;i<channels.length;i++){
var channel = channels[i].channel;
if(channel.channelId===3){
return this;
}
}
}});