Get the App
SLTechnology News&Howtos  ›  Database  › 

Find detailed explanation

Shulou Source: shulou.com Published: 2022-06-01 11:38:06 10月04日 Update

Query an array equivalently:

Db.getCollection ("test") .find ({tags: ["good", "book", "it", "program"]})

(/ / even the order must be the order in which it is stored, which cannot be changed)

Query a value in the array:

Db.getCollection ("test") .find ({tags: "good"})

Query an array of four elements:

Db.getCollection ("test") .find ({tags: {$size: 4}})

Query whether there is a specified field or no specified field and the specified field is null:

Db.getCollection ("test") .insert ([{_ id: 2222, toy: null}, {_ id: 1112}]); db.getCollection ("test"). Find ({_ id: 2222, toy: null}); db.getCollection ("test"). Find (toy: null); / / error db.getCollection ("test"). Find ({_ id: 2222, toy: {$exists: true}}) / / find the current db.getCollection ("test"). Find ({toy: {$exists: true}); / / find the current db.getCollection ("test"). Find ({toy: {$exists: false}}); / / find all the objects that do not have the field toy

Find the return value cursor:

Db.getCollection ("test") .find () .forEach (function (item) {print (item.name, item.price, item.tags);})

Limit,skip method:

Db.getCollection ("test"). Find (). Limit (1) db.getCollection ("test"). Find (). Skip (2)

Query for $in:

Db.getCollection ("test") .find ({_ id: {$in: {12, objectId ("56970120abt538296thg0y6")}}); / / find documentation records where _ id equals 12 or objectId ("56970120abt538296thg0y6")

($in is used for different documents to specify the same Key for or condition matching, and $or can specify multiple Key or condition matching.)

Interval search:

Db.getCollection ("test") .find ({price: {$gt: 3, $lt: 33}}); / / query for values with a price range greater than 3 kids 33. Can be used in document numeric fields or array fields

Tags: Query field array document condition order different price element interval multiple small case numeric method cursor equivalence range if Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Huawei NVidia Microsoft