MongoRecipes
JS
- info
db.version();
db.stats();
db.collection.stats();
db.collection.storageSize()
- Select where field type is string
db.collection.find({field:{$type:2}})
- Update ALL records with field
db.collection.update(
{ field: "old value" },
{ $set: { field: "new value" } },
{upsert: 0, multi: 1} )
- update schema rename column
db.fs.files.update({}, {$rename: {'metadata.Height': 'metadata.height'}}, {multi:true});
db.fs.files.update({}, {$rename: {'metadata.Width': 'metadata.width'}}, {multi:true});
- Check field exists
db.fs.files.find ({ "metadata": {$exists: true} })
db.fs.files.count({ "metadata.creationTimeUtc": {$exists: false}})
db.fs.files.count({ "metadata.Width": {$exists: true}, "metadata.Height": {$exists: true}})
db.createCollection("log", {capped:true, size:100000, max:5000}) /* { "ok" : 1 } */
db.log.isCapped() /* true */
MongoDb C# driver CursorType enumeration 3. MongoDb C# driver wire protocol QueryMessage.TailableCursor 4. QueryMessage.AwaitData Gets a value indicating whether the server should await data (used with tailable cursors).
- bulk insert
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "abc123", defaultQty: 100, status: "A", points: 100 } );
bulk.insert( { item: "ijk123", defaultQty: 200, status: "A", points: 200 } );
bulk.insert( { item: "mop123", defaultQty: 0, status: "P", points: 0 } );
bulk.execute();
- rename database OldDb to DbNew
db.copyDatabase("OldDb","DbNew","localhost")
use OldDb
db.dropDatabase();
- db.collection.insertMany() 5 new in 3.2
GridFS 6
- removal
db.fs.files.remove();
db.getCollection('fs.chunks').remove();
- Most recently updated / modified
db.fs.files.find({filename :/.*.PNG.*/ }).sort({uploadDate:-1});
db.fs.files.find({filename :/.*.JPG.*/ }).sort({"metadata.lastWriteTimeUtc":-1})
- and caused me pain
db.fs.files.count();
db.fs.files.find({length :{"$gt": 0} }).sort({length:-1});
db.fs.files.find({ uploadDate :{"$lt": ISODate("2015-12-15T00:00:00.000Z")} },{"_id":0,"filename":1, uploadDate:1}).sort({uploadDate:-1}).pretty()
db.fs.files.distinct( "filename")
- group by h x w, push filenames, count filenames in each group
db.fs.files.aggregate([
{ $group: {
_id: { "w":"$metadata.width", "h":"$metadata.height" },
files: { $push: "$filename" },
no: { $sum: 1 }
} }
]) ``````````
CLI
mongoexport docs 13, in pairs, export-import, dump-restore connect to powershell ps d:\mongodb\server\3.0\bin>
.\mongoexport -d imagesDB -c fs.files -o 10.11.11.45.fs.files.json .\mongoexport -d imagesDB -c fs.chunks -o 10.11.11.45.fs.chunks.json
Message: connected to: localhost; exported 115 records
mongoimport.exe –db ImageCache10111145 –collection fs.files –file 10.11.11.45.fs.files.json mongoimport.exe –db ImageCache10111145 –collection fs.chunks –file 10.11.11.45.fs.chunks.json
C# Driver
- driver releases 14
- update 15
- old driver cheat sheet 16
- Query by a Field in an Embedded Document 17
- distinct array subdocuments SO 18, api 19
- Using a Tailable Cursor 20
- Configure GridFS Chunksize in MongoDB 21, todo update
- Mongo DB support for Hangfire 22 sources
- Building MongoDB Applications with Binary Files Using GridFS 1 23 and 2 24
- Driver v1.11 25 install tutorial
- Getting started with GridFS driver v2 26
- cursor with filter
using (var cursor = await FsFiles.FindAsync(filter)) {
while (cursor.MoveNext()) {
foreach (var bsonDoc in cursor.Current) {
// bsonDoc.DoSomething();
} } }