The environment

  • Mongo 4.2.0
  • Spring – data – directing 3.0.0
  • jdk1.8

Add and delete

insert …

insertMany …

Common SQL Queries

Return only part of the field :patinet:1

db.getCollection('shouyeshoushu').find({"$or" : [ { "shouyeshoushu.operation_name" : { "$regex" : "^. * the gastrectomy. * $"}}, {" shouyeshoushu. Operation_name" : {" $regex ":" ^. * most of gastric resection. * $"}}, {" shouyeshoushu. Operation_name ": {" $regex ":" ^. * laparoscopic peritoneal biopsy. * $"}}, {" shouyeshoushu. Operation_name ": {" $regex" : "^. * proximal gastric resection. * $"}}, {" shouyeshoushu. Operation_name" : {" $regex ": "^. * all laparoscopic gastric resection. * $"}}, {" shouyeshoushu. Operation_name" : {" $regex ": "^. * endoscopic gastric submucosal decollement. * $"}}, {" shouyeshoushu. Operation_name" : {" $regex ": "^. * endoscopic gastric submucosal decollement. * $"}}, {" shouyeshoushu. Operation_name" : {" $regex ":" ^. * endoscopic gastric submucosal decollement. * $"}}}, {patient_id: 1,visit_id: 1});Copy the code

Only two fields are returned for all data

db.getCollection('zsyyIBD').find({}, {patient_id: 1,visit_id: 1})
Copy the code

Get the minimum value and name it minScore

db.getCollection('jianchabaogao').aggregate(
[{$project: {minScore: {$min: "$jianchabaogao.exam_report.exam_time"}}}]
)
Copy the code

Nested logical operator queries

db.getCollection('zsyyIBD').find( { "$and": [ { "$or": [ { "visit_times": { "$nin": [ "0" ] }, "info_sex": "Female"}}, {$" or ": [{" visit_times" : {" $nin: "(" 0")}, "form_updator" : "qiuyun}]}]})Copy the code

Regular expression matching

db.getCollection('jianchabaogao').aggregate([{ "$or": [ { "jianchabaogao.exam_report.standard_name": { "$regex": "^. *. * $"}}, {" jianchabaogao. Exam_report. Standard_name" : {" $regex ":" $" ^. * scan. *}}]}])Copy the code

Regular expressions in Java code are the same as regular expressions in scripts

switch (fieldRelation) {
    case "Contains":
        return Pattern.compile("^. *"Scan + +". * $", Pattern.CASE_INSENSITIVE);
    case "Not included.":
        return Pattern.compile("^ ((? !" + fieldValue + "). * $", Pattern.CASE_INSENSITIVE);
    case "The beginning is":
        return Pattern.compile("^" + fieldValue + ". * $", Pattern.CASE_INSENSITIVE);
    case "Not at the beginning.":
        return Pattern.compile("^ (? !" + fieldValue + "). *", Pattern.CASE_INSENSITIVE);
    case "Equal":
        return Pattern.compile(fieldValue, Pattern.CASE_INSENSITIVE);
    case "Not equal to.":
        return Pattern.compile("^ (? !" + fieldValue + "). * $", Pattern.CASE_INSENSITIVE);
    case "The end is":
        return Pattern.compile("^. *" + fieldValue + "$", Pattern.CASE_INSENSITIVE);
    case "The end is not.":
        return Pattern.compile("^. * (? !" + fieldValue + "$", Pattern.CASE_INSENSITIVE);
}
Copy the code
 switch (exp) {
        case "Equal":
            Criteria criteriaInnner = criteriaVisitTimes.and(enPath).is(values);
            criteriaInner.add(criteriaInnner);
            break;
        case "Not equal to.":
            Criteria criteriaInnnerNot = criteriaVisitTimes.and(enPath).nin(values);
            criteriaInner.add(criteriaInnnerNot);
            break;
        case "More than":
            Criteria criteriaInnnerGt = criteriaVisitTimes.and(enPath).gt(values);
            criteriaInner.add(criteriaInnnerGt);
            break;
        case "Less than":
            Criteria criteriaInnnerLt = criteriaVisitTimes.and(enPath).lt(values);
            criteriaInner.add(criteriaInnnerLt);
            break;
        case "Greater than or equal to":
            Criteria criteriaInnnerGte = criteriaVisitTimes.and(enPath).gte(values);
            criteriaInner.add(criteriaInnnerGte);
            break;
        case "Less than or equal to":
            Criteria criteriaInnnerLte = criteriaVisitTimes.and(enPath).lte(values);
            criteriaInner.add(criteriaInnnerLte);
            break;
        case "Contains":
            Pattern pattern = Pattern.compile("^. *" + values + ". * $", Pattern.CASE_INSENSITIVE);
            criteriaVisitTimes.and(enPath).regex(pattern);
            criteriaInner.add(criteriaVisitTimes);
            break;
        case "Not included.":
            Pattern patternNot = Pattern.compile("^ ((? !" + values + "). * $", Pattern.CASE_INSENSITIVE);
            criteriaVisitTimes.and(enPath).regex(patternNot);
            criteriaInner.add(criteriaVisitTimes);
            break; }}Copy the code

Post-polymerization cut

db.getCollection('binganshouye').aggregate([ { $group : { _id : { $substr: [ "$_id", 6, 12] }, total : {$sum : 1}}}]);Copy the code

Deletes the value of the field

db.getCollection('bysyweicancer').update(

    {
    },
    
    {
        $unset:{'lab':''}
    },
  
    
    
    {
        "multi" : true
        
    }
);
Copy the code

Pipeline aggregation, relative to time after the aggregation

db.getCollection('jianyanbaogao').aggregate( [ { "$unwind": "$jianyanbaogao.lab_report" }, { "$match": { "jianyanbaogao.lab_report.apply_time": { "$gte": "2014-11-22 11:01:25", "$lte": "$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": {"$match": { "_id": 1, "patient_id": 1, "data": "$jianyanbaogao.lab_report.lab_result_value", "relationData": "$jianyanbaogao.lab_report.apply_time", "lab_sub_item_name":"$jianyanbaogao.lab_report.lab_sub_item_name" } } ] )Copy the code

domain

db.getCollection('jianyanbaogao').distinct('age')
Copy the code

Linux environment commonly used directives

The shell command line is displayed

mongo IP:27017/db_specialized_disease_cohort -u jhadmin -p jh1234
Copy the code
mongo IP:27017/admin -u bysy -p bysyadmin
Copy the code

Rename the collection name:

db.bysyweicancer_malize.renameCollection("bysyweicancer");
Copy the code

Import the Mongo file

Mongoimport -h 127.0.0.1:27017 -ubysy -pbysyadmin --authenticationDatabase admin -d zbk2021-2 /data/1/sddap3/bysyweicancer.json

Example Export the Mongo file

Mongoexport -h 127.0.0.1:27017 -ubysy -pbysyadmin --authenticationDatabase admin -d zbk2021-2 -c bysyweicancer -o /data/1/sddap3/bysyweicancer.json

Insert the query results as new data into the new table

var result = db.getCollection('shouyeshoushu').find({"$or": [{"shouyeshoushu.operation_name" : { "$regex" :  "^.* Gastrectomy.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Gastrectomy.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Laparoscopic peritoneal biopsy.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Proximal gastrectomy.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Laparoscopic total gastrectomy.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Endoscopic submucosal dissection of the stomach.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Endoscopic submucosal dissection of the stomach.*$"}},
{ "shouyeshoushu.operation_name" : { "$regex" :  "^.* Endoscopic submucosal dissection of the stomach.*$"}}
]
}, {patient_id: 1});
Copy the code
while(result.hasNext()) db.shouyeshoushu_end.insert(result.next())
Copy the code

Other imports and exports:

mongorestore -h 127.0. 01.:20000 -ubysy -pbysyadmin --authenticationDatabase admin -d zbknew /home/keyan/bysyweicancer.bson

mongodump -u bysy -p bysyadmin --host 127.0. 01. --port 27017 --authenticationDatabase admin -d zbk2021-2 -o /data/1

mongorestore -h 127.0. 01.:20000 -d zbknew /home/keyan/bysyweicancer.bson
Copy the code

Q&A

1. Online mongo large page number error, can not be normal paging

Caused by: com.mongodb.MongoQueryException: Query failed with error code 96 and error message 'Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.' on server 168.168.251.48:20000

The reason:

Mongo is to fetch all the data, in memory paging sort, the default setting of 32M, it is easy to lead to insufficient memory

Solution:

Set the mongo library sort maximum memory to 320M:

db.adminCommand({setParameter:1,internalQueryExecMaxBlockingSortBytes:335544320});

To index high-frequency, core fields (especially sorted fields) :

db.zsyynxgbzbk.createIndex({"baseinfolast_lastvisittime":1});

2. Deep pit: Code written in the sort field in the mongo table does not have,mongo does not report error

query.with(Sort.by(Sort.Order.desc("lastvisittime"))); // The library field is baseinfolast_lastvisitTime

2. Mongo JSON file, copy to the local Win10 environment, and then use tools to import problems