MongoDB Basic Command Summary
MONGO additions and deletions change
Batch update based on query criteria
> DB. Goodsinfo.update ({"Type": 1}, {$set: {"name": "Birdben"}}, False, True);
Delete data for a specified condition
> DB. Goodsinfo.remove ({goodsstatus:1});
Query modification Delete
> DB. Goodsinfo.findandmodify ({
query: {price: {$gte: +},
sort: {Price:-1},
update: {$set: {name: ' Birdben '} , $inc: {price:10}},
remove:true
});
> Db.runcommand ({findandmodify: "Goodsinfo",
query: {price: {$gte: +}},
sort: {Price:-1},
Update: {$set: {name: ' Birdben '}, $inc: {price:10}},
remove:true
});
MONGO Query
Query the specified column (view name, price data only)
Db. Goodsinfo.find ({"_id": "123456"}, {"Name": 1, Price:1}}). Pretty ();
Of course name can also be used with true or false, as in the case of Ture River Name:1 effect, if False is to exclude name, display column information other than name.
Distinct usage
Db. Goodsinfo.distinct ("name");
Filter the results of distinct
Interval query
Db. Goodsinfo.find ({price: {$gte: +, $lte: 200}});
Like fuzzy query
The name contains the MONGO data
db. Goodsinfo.find ({name:/mongo/});
Data db that begins with MONGO
. Goodsinfo.find ({name:/^mongo/});
or query
Db. Goodsinfo.find ({$or: [{price:100}, {price:200}]});
exists query
Db. Goodsinfo.find ({price: {$exists: true}});
Query the number of exists
db. Goodsinfo.find ({price: {$exists: true}}). Count ();
Querying sub-documents
Db. Goodsinfo.find ({"Array.key": "Value"});
Group queries
Group query for a time period, according to the main paste grouping, statistics of the number of replies per main Post db. Postinfo.aggregate ([{$match: {"Createtime": {"$gt": 1443628800000, "$lt": 1446436799000}, "Srcpostid": {$exists: true}
}}, {$group: {_id: "$srcPostId", total: {$sum: 1}}}, {$sort: {total:-1}}]); Statistics are based on the reason for rejection, the number of each reason, and the number of filtered results is greater than or equal to 5 db. Reviewrecord.aggregate ([{$group: {_id: ' $rejectReason ', Count: {$sum: 1}}}, {$sort: {count:-1}}, {
$limit: Ten}, {$match: {count: {$gte: 5}}]); After grouping, in the number of statistics, the number of statistics after grouping is greater than or equal to 5 of how many db. Reviewrecord.aggregate ([{$group: {_id: {rejectreason: ' $rejectReason '}, Count:
{$sum: 1}}, {$match: {count: {$gte: 5}}}, {$group: {_id:null, Count: {$sum: 1}
}
}
] );
Similar to Sql:select count (*) from (SELECT Rejectreason, Count from Reviewrecord GROUP by Rejectreason); Complex statistical calculations that count the total number of users per city in 2015-10-29, number of new users, number of old users, number of unusual users Db.runcommand ({"group": {"ns": "LandpushtypecoUnt "," key ": {" Cityid ": true}," initial ": {codecount:0, newcount:0, oldcount:0, errorcount:0}," $reduce ": function
(Doc,prev)
{prev.totalcount++;
if (doc.isnew = = 0) {prev.newcount++;
} else {prev.oldcount++;
} if (doc.isoldequipmement = = 1) {prev.errorcount++; }}, "condition": {"date": "2015-10-29", "Cityid": {$in: ["110000", "330100"]}}});
Reference article: http://www.open-open.com/lib/view/open1392709240428.html http://docs.mongoing.com/manual-zh/core/ Single-purpose-aggregation.html http://docs.mongoing.com/manual-zh/reference/sql-aggregation-comparison.html