= MongoDB = == Query by data type == Select all documents in the inventory collection where the price field value is a Double. {{{#!highlight javascript db.inventory.find( { price: { $type : 1 } } ) }}} Select all documents in the inventory collection where the datex field value is a Date. {{{#!highlight javascript db.inventory.find( { datex: { $type : 9 } } ) }}} == Data types == ||Type||Number|| ||Double||1|| ||String||2|| ||Object||3|| ||Array||4|| ||Binary data||5|| ||Undefined (deprecated)||6|| ||Object id||7|| ||Boolean||8|| ||Date||9|| ||Null||10|| ||Regular Expression||11|| ||JavaScript||13|| ||Symbol||14|| ||JavaScript (with scope)||15|| ||32-bit integer||16|| ||Timestamp||17|| ||64-bit integer||18|| ||Min key||255|| ||Max key ||127|| == PHP driver == === Manual install === * cd /tmp * wget http://pecl.php.net/get/mongo-1.4.3.tgz * tar xvzf mongo-1.4.3.tgz * yum install php-devel #on CentOS * phpize * ./configure * make * make install * add '''extension=mongo.so''' to /etc/php.ini * In CentOS 64 bit the '''mongo.so''' should be located in /usr/lib64/php/modules/mongo.so == Aggregation examples == http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/ === Example 1 === {{{#!highlight sql SELECT country,city,min(timestampx) as minx,max(timestampx) as maxx,count(*) as countx FROM collx GROUP BY country,city WHERE idxyz=1234 AND timestampx>='2011-06-04T00:00:00' AND timestampx<='2012-12-04T23:59:59' ORDER BY minx }}} {{{#!highlight javascript db.collx.runCommand( { "aggregate":"collx" , "pipeline":[ {"$match": { idxyz:1234 , "timestampx":{ "$gte":ISODate("2011-06-04T00:00:00") , "$lte":ISODate("2012-12-04T23:59:59") } } } , {"$group": { "_id": {"country":"$country","city":"$city"} , "minx":{"$min":"$timestampx"} , "maxx":{"$max":"$timestampx"} , "countx":{"$sum":1} } } , { "$sort": { "minx":1 } } ] } ) }}} {{{#!highlight javascript db.collx.aggregate( [ {"$match": { idxyz:1234 , "timestampx":{ "$gte":ISODate("2011-06-04T00:00:00") , "$lte":ISODate("2012-12-04T23:59:59") } } } , {"$group": { "_id": {"country":"$country","city":"$city"} , "minx":{"$min":"$timestampx"} , "maxx":{"$max":"$timestampx"} , "countx":{"$sum":1} } } , { "$sort": { "minx":1 } } ] ) }}} {{{#!highlight javascript db.collx.runCommand( { "aggregate":"collx" , "pipeline":[ {"$group": { "_id": {"fld":"$fldx"} , "countx":{"$sum":1} } } , { "$sort": { "countx":1 } } ] } ).result }}} == Get creation timestamp from document == Inside the creation timestamp is stored in the _id field . Using the method getTimestamp on an ObjectiId, is possible to get the creation timestamp. {{{#!highlight javascript // runned inside mongo shell ObjectId("52840c4facb4f200d8a6518a").getTimestamp() // returned ISODate("2013-11-13T23:33:35Z") }}} == Update field on document == {{{#!highlight javascript db.products.update( { sku: "abc123" }, { $set: { quantity: 500, instock: true } } ) }}} == Find documents that don't have a field == {{{#!highlight javascript db.products.find( { fieldx:{$exists:false} } ) }}} == Find docs with status equal Failure and only show the _id and status fields == {{{#!highlight javascript db.products.find( {status:"Failure"} , { status:1 } ) }}} == Set journal small == {{{#!highlight bash # smallfiles=true to the config file /etc/mongodb.conf. service mongodb stop cd /var/lib/mongodb/journal rm * service mongodb start mongo }}} == Sort by field == * descending * db.rxtx.find().sort({timestamp:0}).limit(10) * ascending * db.rxtx.find().sort({timestamp:1}).limit(10)