简体   繁体   中英

MongoDB, find items matching the term “a” & $nin

i'm trying, to figure out how can i make this query work in mongodb.

This is the query i'm trying to do ( "translated" to mysql )

SELECT title FROM artists WHERE name LIKE "%a%" AND id NOT IN(1, 2)

The try in mongoDB

$term = new MongoRegex('/^a/i')l;

$not = array('$nin' => array('_id' => array('1', '2')));

  1. $artists->find(array('name' => $term), $not);
  2. $artists->find(array('name' => $term, $not)) ;

how can i make this work as expected, right now it returns null

Here is a solution for mongodb

db.artists.find({
   "name" : /a/,
   "id" : {
        "$nin" : [1,2]
   }
})

just by writing each {} as an array, you will get the answer for php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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