English 中文(简体)
MongoDB和PHP的 Qu阵列
原标题:Querying nested arrays in MongoDB & PHP
  • 时间:2011-11-22 11:16:58
  •  标签:
  • php
  • mongodb

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)).

解决你的问题,可作如下计划:

{
 _id: 234234234324,
 accounts: [
    {
        enabled: 1,
        name:  asdf ,
        id: 0
    },
    {
        enabled: 0,
        name:  gfsd ,
        id: 1
    }]
}

如果你作出上述改动,你将提出疑问。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签