Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Find detailed explanation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report