MongoDB: How to find out if an array field contains an element?

前端 未结 3 1264
甜味超标
甜味超标 2020-12-02 10:59

I have two collections. The first collection contains students:

{ \"_id\" : ObjectId(\"51780f796ec4051a536015cf\"), \"name\" : \"John\" }
{ \"_id\" : Object         


        
3条回答
  •  温柔的废话
    2020-12-02 11:27

    I am trying to explain by putting problem statement and solution to it. I hope it will help

    Problem Statement:

    Find all the published products, whose name like ABC Product or PQR Product, and price should be less than 15/-

    Solution:

    Below are the conditions that need to be taken care of

    1. Product price should be less than 15
    2. Product name should be either ABC Product or PQR Product
    3. Product should be in published state.

    Below is the statement that applies above criterion to create query and fetch data.

    $elements = $collection->find(
                 Array(
                    [price] => Array( [$lt] => 15 ),
                    [$or] => Array(
                                [0]=>Array(
                                        [product_name]=>Array(
                                           [$in]=>Array(
                                                [0] => ABC Product,
                                                [1]=> PQR Product
                                                )
                                            )
                                        )
                                    ),
                    [state]=>Published
                    )
                );
    

提交回复
热议问题