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}}) 
    
  • capped collection with a tailable cursor
      db.createCollection("log", {capped:true, size:100000, max:5000}) /* { "ok" : 1 } */
      db.log.isCapped() /* true */
    

    MongoDb C# driver CursorType enumeration. MongoDb C# driver wire protocol QueryMessage.TailableCursor. 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() new in 3.2

GridFS

  • 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 } 
          } } 
      ])				``````````
    
  • old c# api v1
  • upload files 2.2, 2.4
  • Download files
  • Find files
  • Delete and rename

CLI

mongoexport docs, 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#

« | home | wiki