MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

  • MongoDB

Contents

  1. MongoDB
    1. Query by data type
    2. Data types
    3. PHP driver
    4. Aggregation examples
    5. Get creation timestamp from document
    6. Update field on document
    7. Find documents that don't have a field
    8. Find docs with status equal Failure and only show the _id and status fields
    9. Set journal small
    10. Sort by field

MongoDB

Query by data type

Select all documents in the inventory collection where the price field value is a Double.

   1 db.inventory.find( { price: { $type : 1 } } )

Select all documents in the inventory collection where the datex field value is a Date.

   1 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

   1 SELECT country,city,min(timestampx) as minx,max(timestampx) as maxx,count(*) as countx
   2 FROM collx 
   3 GROUP BY country,city 
   4 WHERE idxyz=1234 AND timestampx>='2011-06-04T00:00:00' AND timestampx<='2012-12-04T23:59:59'
   5 ORDER BY minx

   1 db.collx.runCommand( 
   2   {  "aggregate":"collx" , "pipeline":[
   3       {"$match": 
   4          {
   5            idxyz:1234 , 
   6            "timestampx":{ 
   7              "$gte":ISODate("2011-06-04T00:00:00") , 
   8              "$lte":ISODate("2012-12-04T23:59:59") 
   9            }   
  10          } 
  11       } 
  12       ,
  13       {"$group": 
  14          {
  15            "_id":  {"country":"$country","city":"$city"} , 
  16            "minx":{"$min":"$timestampx"} , 
  17            "maxx":{"$max":"$timestampx"} ,  
  18            "countx":{"$sum":1}  
  19          }    
  20       }
  21       ,
  22       {
  23         "$sort":
  24         { 
  25           "minx":1
  26         } 
  27       } 
  28     ]       
  29   }   
  30 )

   1 db.collx.aggregate( 
   2      [
   3       {"$match": 
   4          {
   5            idxyz:1234 , 
   6            "timestampx":{ 
   7              "$gte":ISODate("2011-06-04T00:00:00") , 
   8              "$lte":ISODate("2012-12-04T23:59:59") 
   9            }   
  10          } 
  11       } 
  12       ,
  13       {"$group": 
  14          {
  15            "_id":  {"country":"$country","city":"$city"} , 
  16            "minx":{"$min":"$timestampx"} , 
  17            "maxx":{"$max":"$timestampx"} ,  
  18            "countx":{"$sum":1}  
  19          }    
  20       }
  21       ,
  22       {
  23         "$sort":
  24         { 
  25           "minx":1
  26         } 
  27       } 
  28     ]       
  29 )

   1 db.collx.runCommand(  { "aggregate":"collx" , "pipeline":[
   2       {"$group": { "_id":  {"fld":"$fldx"} , "countx":{"$sum":1}  }   }
   3       ,
   4       { "$sort": { "countx":1  } } 
   5 ] } ).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.

   1 // runned inside mongo shell
   2 ObjectId("52840c4facb4f200d8a6518a").getTimestamp()
   3 // returned ISODate("2013-11-13T23:33:35Z")
   4 

Update field on document

   1 db.products.update( 
   2 { sku: "abc123" },
   3 { $set: { quantity: 500,  instock: true  }    }
   4 )

Find documents that don't have a field

   1 db.products.find( { fieldx:{$exists:false} } )

Find docs with status equal Failure and only show the _id and status fields

   1 db.products.find( {status:"Failure"} , { status:1  } )

Set journal small

   1 # smallfiles=true to the config file /etc/mongodb.conf.
   2 service mongodb stop
   3 cd /var/lib/mongodb/journal
   4 rm *
   5 service mongodb start
   6 mongo 

Sort by field

  • descending
    • db.rxtx.find().sort({timestamp:0}).limit(10)
  • ascending
    • db.rxtx.find().sort({timestamp:1}).limit(10)
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01