Mongodb operation notes

Basic operation

Like the query

The getCollection (' test_document). Find ({billNo: {$regex:/ 0601 /}}) the getCollection (' test_document). Find ({billNo: {$regex:"^ 0701 *"}})
Copy the code

In the query

{billNo: {$in: ['10086', '10010']}}Copy the code

Date query

{' updateDate ':{g te: I S O Date ("2019? 12? 26 T 23 : 59 : 59 Z "),gte:ISODate("2019-12-26T23:59:59Z"),gte:ISODate("2019? 12? 26T23:59:59Z"),lt: ISODate ("2019-12-27T23:59: 59 z ")}}Copy the code

Update the field

Db.getcollection (' test_document ').update({" _id ":"10086"}, {$set: {" status ":"0, "statusName" : "Draft"}}, {" multi ":false,
“upsert” : false});Copy the code

Query: The query condition is similar to where in the SQL update query. Update: An update object and some update operators (e.g.,,inc…) Insert objNew (true) if no update record exists, default (false) if no update record exists. Multi: Optional. By default, mongodb updates only the first found record. If this parameter is true, mongodb updates all found records.

The sorting

The getCollection (' test_document). Find ({" auditTime ": {$gte: ISODate ("2019-05-01T00:00: 00 z "),$lt: ISODate ("2019-05-16T12:00: 00 z ")}}) sort ({' billNo:1})

Copy the code

The exists query

db.records.find( { b: { $exists: false}}) db.getCollection(' test_document ').find({' billinfos.payoverdate ': {$exists: true },‘invoiceDate’: { $exists: false }})
Copy the code

Is not equal to $ne

db.things.find( { x : { $ne : 3}});Copy the code

Grouping query

db.test_document.aggregate( [
{ $match: {" type ":"20'}}, {$group: {
_id: “$headerId”,
total: { $sum: 1}}}, {$match: { total: { $gt: 2}}}])Copy the code

Delete records

Db. Test_document. Remove ({" billNo ":"10086'})Copy the code

Mongodb Export Records

mongoexport -h 127.0. 01. --port 27017-u test -p test123 Connect to the database -d mepscommon specifies the name of the database -c bills specifies the name of the document. Query different documents can be replaced with the document name you need -f "billNo, createdDate, createdBy, createdName, status, the deptName" you need to export the field name, if there are two layers of structure, level 1 point secondary field. Such as billInfos. Status - q "{" createdDate" : {" $gte ": {$date: '2019-05-09T08:00: 00} z ', '$lt' : {$date: '2019-05-09T08:59:59Z '}}} "Query filter criteria - type= CSV file format, which is generally exported to CSV format -o D:\test\20190508.CSV output file address and file nameCopy the code