PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] what is the equivalent query from mysql to mongosb

 January 16, 2022     composer-php, mongodb, php     No comments   

Issue

im new to mongodb after years with mysql and trying to figure this equivalent query in mongodb with php composer

select * from table where (x > 0 and x < 30) or x = 'half' and sid = 1

$query = [
    '$and' => [
        [
     'sid'=> 1
        ], ['$and' => [[
            'info.x' => [
                '$lt' => '30'
            ]
        ], [
            'info.x' => [
                '$gt' => '0'
            ]
        ],  [
            'info.x' => [ /// i want to put this in $or
                'half'
            ]
        ]]
]

    ]
];

i want to display all what is greater then 0 and less then 30 or equal to 'half' where sid is 1

thanks


Solution

It must be

db.collection.find({
  "$or": [
    { "x": { "$gte": 0, "$lte": 30 }},
    { "x": "half" }
  ],
  "sid": 1
})

Or

$query = [
  '$or'=> [
    [ 'x'=> [ '$gte'=> 0, '$lte'=> 30 ]],
    [ 'x'=> 'half' ]
  ],
  'sid'=> 1
]


Answered By - Ashh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing