简体   繁体   English

PHP:具有位置运算符的嵌入式文档数组上的MongoDB投影

[英]PHP: MongoDB projection on embedded document array with positional operator

I want to retrieve a specific array from embedded document on matching base but either the output is displaying all arrays in that document not a specific one or displaying just the first array.... this is my code please have a look and answer this according to PHP. 我想从匹配基础上的嵌入式文档中检索特定数组,但是输出显示该文档中的所有数组而不是特定数组,或者仅显示第一个数组。...这是我的代码,请根据以下内容进行查找和回答到PHP。

$dept->findOne(['$and' => [
    ['_id' => 'd004'],
    ['dept_employees.from_date' => '1986-12-01'],
    ['dept_employees.to_date' => '9999-01-01']
                         ]
    ],
    ['projection' => ['dept_employees.$' => 1] ]);

this is the output 这是输出

 MongoDB\\Model\\BSONDocument::__set_state(array( '_id' => 'd004', 'dept_employees' => MongoDB\\Model\\BSONArray::__set_state(array( 0 => MongoDB\\Model\\BSONDocument::__set_state(array( 'emp_id' => '10003', 'from_date' => '1995-12-03', 'to_date' => '9999-01-01', )), )), )) 

the document is like this have 247 sub-arrays 像这样的文档有247个子数组

 MongoDB\\Model\\BSONDocument::__set_state(array( '_id' => 'd004', 'dept_employees' => MongoDB\\Model\\BSONArray::__set_state(array( 0 => MongoDB\\Model\\BSONDocument::__set_state(array( 'emp_id' => '10003', 'from_date' => '1995-12-03', 'to_date' => '9999-01-01', )), 1 => MongoDB\\Model\\BSONDocument::__set_state(array( 'emp_id' => '10004', 'from_date' => '1986-12-01', 'to_date' => '9999-01-01', )), 2 => MongoDB\\Model\\BSONDocument::__set_state(array( 'emp_id' => '10010', 'from_date' => '1996-11-24', 'to_date' => '2000-06-26', )), 

and i want only this 我只想要这个

 MongoDB\\Model\\BSONDocument::__set_state(array( '_id' => 'd004', 'dept_employees' => MongoDB\\Model\\BSONArray::__set_state(array( 1 => MongoDB\\Model\\BSONDocument::__set_state(array( 'emp_id' => '10004', 'from_date' => '1986-12-01', 'to_date' => '9999-01-01', )), 

Dates are here only as strings, i am just matching strings. 日期在这里只是字符串,我只是在匹配字符串。 The shell is also displaying same results: 该外壳程序也显示相同的结果:

db.departments.find( {"dept_employees.from_date": '1986-12-01',    
"dept_employees.to_date": '9999-01-01'}, {"dept_employees.$":1} ).pretty()
{
        "_id" : "d004",
        "dept_employees" : [
                {
                        "emp_id" : "10003",
                        "from_date" : "1995-12-03",
                        "to_date" : "9999-01-01"
                }
        ]
}

According to MongoDB documentation : 根据MongoDB文档:

The positional $ operator limits the contents of an from the query results to contain only the first element matching the query document. 位置$运算符将查询结果中的内容限制为仅包含与查询文档匹配的第一个元素。

and : 和:

See the aggregation operator $filter to return an array with only those elements that match the specified condition. 请参阅聚合运算符$ filter返回仅包含与指定条件匹配的那些元素的数组。

https://docs.mongodb.com/manual/reference/operator/projection/positional/ https://docs.mongodb.com/manual/reference/operator/projection/positional/

https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#exp._S_filter https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#exp._S_filter

Also, not exactly sure in your case, but if you are looking to match date ranges, maybe using '$gt' and '$lt' can help. 另外,不确定情况,但如果要匹配日期范围,也许使用'$ gt'和'$ lt'可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM