Solve MongoDB $in needs an array problem

Source: Internet
Author: User
Tags mongodb query

Problem phenomenon:

Throws an exception when MongoDB performs a bulk query operation Exception 2: $in needs an array.

Problem solving:

Thanks to the great Google and StackOverflow someone has encountered this problem, the cause of the problem explained very clearly, steal a lazy, direct copy come over, as follows:

This ... is a change in MongoDB 2.6.0, no longer accepting BsonObjectIn the$inClause.This particular issue is being tracker asA PHP driver bug at https://jira.mongodb.org/browse/php-1051The MongoDB PHP Driver wouldSerializeAn PHPArrayInto BSONArray(Accepted by the$inoperator) when the PHPArrayIs:sequential numerically indexed, starting from 0This meansifYou have anArrayLike$array=Array($id 0,$id 1,$id 2,$id 3,$id 4);unset($array[0]); You actually wind up with:$array=Array(1 =$id 1, 2 =$id 2, 3 =$id 3, 4 =$id); Which does not start with index0. The MongoDB PHP driver therefore converts this to a BSONObject... leading to validation error in MongoDB asIt expected anArray. Now, since the MongoDB PHP driver does not DoParse your MongoDB query we cannot know whichArrayShould is exempted from this serialization rule.The workaround is, asmentioned above, is to ensure your PHP arrays was numerically indexed, starting from 0. The easiest to DoThat's to runarray_values($values);

My English level general, belongs to the basic ability to understand the type, a little explanation, from 2.6.0 later, MongoDB in the use of $in/xxx This form of query, no longer support Bson object, and an array, if the index is not beginning with 0, then the MongoDB of PHP The driver will turn it into a Bson object by default, causing the bulk query to fail to query.

The person who said the question also said how to solve the problem by using Array_values ($values) to generate a new array with an index starting at 0.

Solve MongoDB $in needs an array problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.