Get the App
SLTechnology News&Howtos  ›  Database  › 

How to read data operation in Mongodb

Shulou Source: shulou.com Published: 2022-05-31 21:09:35 09月22日 Update

Mongodb how to read data operation, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Mongodb read data operation

There are two ways for Mongodb to read data (document documents)

> find ()

> findOne ()

The find () method is the primary method, and the find method returns a cursor that points to the query result set data.

The standard command is: db.collection.find (,)

Which is equivalent to the where conditional statement in the sql statement

Equivalent to the field to be taken out.

It's important to note here that if you don't specify query, you mean finding out all the data.

You can also query the case of multiple values for a condition

Eg:db.test.find ({_ id: {$in: [1565 Eg:db.test.find Objectid ("564b408cf3e596a45ed9d243")]}})

A slightly more complex query:

Db.test.find ({awards: {$elemMatch: {award: 'test',year: {$gt: 2000}})

This means to query all data in the awards array where the award field is test and the year field is greater than 2000; where $gt means (greater than)

What's interesting is that the query order of mongodb has to be the same as that in the data.

For example, this query:

> db.test.find ({name: {first:'aaa',last:'bbb'}})

If what is stored in the database is {last:'bbb',first:'aaa'}, it can not be found out; remember!

If you want to check that name's first is aaaa,name and last is bbbb, you can do this.

Db.test.find ({'name.first':'aaa','name.last':'bbb'}) uses the object's "." Do it this way, which is why the name of the data field of mongdb cannot contain $and. One of the reasons. This is its special reserved grammar key character.

If you want to check with the "or" condition, use the keyword $or

> db.test.find ({$or: [{'name.last':/ ^ dd/}, {birth: {$lt: newDate (' 01 name.last':/ 1990')}}]})

This query means: query the dd-started data of name's last, or find out the data of birth joke 1900-01-01. $lt (lessthan abbreviation)

Specify which fields are returned through the

Db.bios.find ({}, {name: 1, contribs: 1, _ id: 0})

1 indicates that this data is returned as a result, and 0 means that the data is filtered out. _ id will return by default unless it shows write 0 and specifies that it will not be returned

The Find method returns a cursor pointing to the data collection, which has

Next, hasNex,forEach method

Traversing all the results can be used like this:

Var cur = db.test.find (); cur.forEach (printjson)

Or while (cur.hasNext ()) printjson (cur.next ())

If you want to sort, use sort ()

Eg:db.test.find () .sort ({_ id:1}); 1 for asc, 0 for desc

Use limit () if you specify how many

Eg:db.test.find (). Limit (5) means that only the first five

There is also a skip () method to remove the first few.

And findOne (), as the name implies, is a special case of the find () method, which only returns the first

After reading the above, have you mastered the method of how to read data in Mongodb? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data method query field meaning condition cursor result key keyword content reason direction more statement problem complex special helpless as the name implies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi vpn Shulou Tech Info Apple