MongoDB has many advantages, such as multi-column indexes. Some statistical functions can be used for queries and multi-condition queries are supported. However, multi-table queries are not currently supported, you can try to solve the problem of multi-Table query through data redundancy.
MongoDB provides a wealth of data operations. The following are some examples. Most of the content is from the official documentation, and others are for your understanding.
Query all colls data
DB. colls. Find () // select * From colls
Query by specified conditions
DB. colls. Find ({'last _ name': 'Smith '}); // select * From colls where last_name = 'Smith'
Specify multi-condition Query
DB. colls. Find ({X: 3, Y: "foo"}); // select * From colls where x = 3 and Y = 'foo'
Query by specified condition range
DB. colls. Find ({J: {$ ne: 3}, K: {$ GT: 10}); // select * From colls where J! = 3 and K> 10
The query does not include any content.
DB. colls. Find ({}, {A: 0}); // query all data except that A is 0.
Supported <, <=,>,> =, and must be replaced by $ lt, $ LTE, $ GT, and $ GTE.
DB. colls. Find ({"field": {$ GT: Value }});
DB. colls. Find ({"field": {$ LT: Value }});
DB. colls. Find ({"field": {$ GTE: Value }});
DB. colls. Find ({"field": {$ LTE: Value }});
You can also query the range of a field.
DB. colls. Find ({"field": {$ GT: value1, $ LT: value2 }});
Not equal to $ ne
DB. colls. Find ({x :{$ ne: 3 }});
Character $ in for in Query
DB. colls. Find ({"field": {$ in: array }});
DB. colls. Find ({J: {$ in: [2, 4, 6]});
Not in query character $ Nin
DB. colls. Find ({J: {$ Nin: [2, 4, 6]});
MoD query character $ mod
DB. colls. Find ({A: {$ mod: [10, 1]}) // Where a % 10 = 1
$ All Query
DB. colls. Find ({A :{$ All: [2, 3] }}); // when specifying a to satisfy any value in the array
$ Size Query
DB. colls. Find ({A: {$ size: 1}); // queries the number of objects. This query queries records with the number of sub-objects of a being 1.
$ Exists Query
DB. colls. Find ({A: {$ exists: true}); // data of object
DB. colls. Find ({A :{$ exists: false }}); // The data of object A does not exist.
$ Type query $ the type value is the unique value of bsonhttp: // bsonspec.org/data
DB. colls. Find ({A: {$ type: 2}); // match data of the string type as
DB. colls. Find ({A: {$ type: 16}); // match a as int type data
Match with regular expressions
DB. colls. Find ({Name:/acme. * Corp/I}); // similar to like
Embedded object query
DB. colls. Find ({"author. Name": "Joe "});
Version 1.3.3 and later include $ not query
DB. colls. Find ({Name: {$ not:/acme. * Corp/I }});
DB. colls. Find ({A :{$ not :{$ mod: [10, 1] }}});
Sort () sorting
DB. colls. Find (). Sort ({ts:-1}); // 1 is in ascending order. 2 is in descending order.
Limit () Limit the number of returned data queries
DB. colls. Find (). Limit (10)
Skip () skips some data
DB. colls. Find (). Skip (10)
Snapshot () snapshot ensures that no duplicate data is returned or objects are lost
Count () count the number of queried objects
DB. Students. Find ({'address. state': 'CA'}). Count (); // High Efficiency
DB. Students. Find ({'address. state': 'CA'}). toarray (). length; // very inefficient
The group () function for query result grouping is similar to the group by function in SQL.
Distinct () returns a non-repeated Value
From the above, we can see that there are many ways to query MongoDB, which can be used in combination. The common sum statistics function is not currently supported by MongoDB.
Java code
Querybuilder = new querybuilder (); querybuilder. And (key). Is (value );
You can also use querybuilder. Start (key). Is (value). Get () to obtain a dbobject object for query.
The above key is the key to be queried, and the value is the matched value.
Then use
Java code
Collection. Find (querybuilder. Get ())
Query
The query result is dbcusor.
You can use querybuilder. And (key). Is (value). And (key). Is (value) to query multiple key values.
And, the available query methods include lessthan, equals, exists, greaterthan, greaterthanequals, In, lessthan, lessthanequals, Mod, notequals, notin, and RegEx.