I’m using MongoDB with PHP’s own driver and I have saved multiple arrays under main document. Structure looks like this:
{
_id: 234234234324,
accounts: {
0: {
enabled: 1,
name: 'asdf'
},
1: {
enabled: 0,
name: 'gfsd'
}
}
Accounts are removed and added by the user so I have no control over the array key. I need to somehow get all accounts that have enabled: 1. I tried array(“accounts.enabled”=>1) but it doesn’t work. And suggestions?
In your schema accounts isn’t nested array, it’s actually complex object, because of this
array("accounts.enabled"=>1)will not work (you actually can check only particular element ->array("accounts.0.enabled"=>1)).Solution of your problem can be schema redesign as follows:
If you will make above changes, your query will work.