MongoDB-基本命令学習1

7699 ワード

MongoDB-基本命令学習1
MongoDB   /     
> use fyluse fyl  (use dbname          ,  show dbs    ,        )
switched to db fyl

> show dbsshow dbs
blog   0.000GB
local  0.000GB
test   0.000GB
> 
> db.fyl.insert({name:"fyl",age:11})db.fyl.insert({name:"fyl",age:11})
WriteResult({ "nInserted" : 1 })
> 
> show dbsshow dbs
blog   0.000GB
fyl    0.000GB
local  0.000GB
test   0.000GB

> db.dropDatabase()db.dropDatabase()  (     , use dbname,    db.dropDatabase()         )
{ "dropped" : "fyl", "ok" : 1 }
> 
> show dbsshow dbs
blog   0.000GB
local  0.000GB
test   0.000GB


MongoDB     
db.COLLECTION_NAME.insert(document)

          MongoDB   runoob       col   :
db.col.insert({title: 'MongoDB   ', 
    description: 'MongoDB     Nosql    ',
    by: '    ',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})


         
document=({title: 'MongoDB   ', 
    description: 'MongoDB     Nosql    ',
    by: '    ',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
});

db.col.insert(document)

           db.col.save(document)   。      _id    save()       insert()   。     _id   ,      _id    。


MongoDB     
update()   
update()             。      :
db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)
    :
query : update     ,  sql update   where   。
update : update            ( $,$inc...) ,      sql update   set   
upsert :   ,        ,     update   ,    objNew,true   ,   false,   。
multi :   ,mongodb    false,           ,       true,                。
writeConcern :  ,       。


db.col.update({'title':'MongoDB   '},{$set:{'title':'MongoDB'}})
                ,             ,      multi     true。
db.col.update({'title':'MongoDB'},{$set:{'title':'MongoDB-fyl'}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
db.col.find().pretty()

save()   
save()                 。      :
db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)
    :
document :     。
writeConcern :  ,       。


  
           _id   571991355cd37533a0cf711c      :
db.col.save({
	"_id" : ObjectId("571991355cd37533a0cf711c"),
    "title" : "fyl",
    "description" : "MongoDB",
    "by" : "Runoob",
    "url" : "http://www.runoob.com",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 11
})

    
        :
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
    :
db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
      :
db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
       :
db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
    :
db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
        :
db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );


MongoDB     

  
remove()              :
db.collection.remove(
   <query>,
   <justOne>
)
     MongoDB   2.6      ,      :
db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)
    :
query :(  )        。
justOne : (  )     true   1,        。
writeConcern :(  )       。

  2   
db.fyl.insert({title: 'MongoDB', 
    description: 'Nosql    ',
    by: '  ',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 99
})
  
 db.fyl.find().pretty()db.fyl.find().pretty()
{
        "_id" : ObjectId("571997885cd37533a0cf711e"),
        "title" : "MongoDB",
        "description" : "Nosql    ",
        "by" : "  ",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("571997915cd37533a0cf711f"),
        "title" : "MongoDB",
        "description" : "Nosql    ",
        "by" : "  ",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 99
}

  
db.fyl.remove({"likes" : 99})
WriteResult({ "nRemoved" : 1 })
db.fyl.remove({"likes" : 99})db.fyl.find().pretty()db.fyl.find().pretty()
{
        "_id" : ObjectId("571997885cd37533a0cf711e"),
        "title" : "MongoDB",
        "description" : "Nosql    ",
        "by" : "  ",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}

                    justOne   1,    :
db.fyl.remove({"likes" : 99},{justOne: true})
          ,        (     SQL   truncate   ):
>db.col.remove({})
>db.col.find()


MongoDB     
  
MongoDB            :
>db.COLLECTION_NAME.find()
find()                  。
                ,     pretty()   ,      :
>db.col.find().pretty()
pretty()                 。

db.col.find({"likes" : {$ne:100}}).pretty()

MongoDB AND   
MongoDB   find()          (key),   (key)     ,    SQL   AND   。
      :
>db.col.find({key1:value1, key2:value2}).pretty()
  
       by   likes      
> db.col.find({"by":"    ", "likes" : 100}).pretty()
         WHERE   :WHERE by='    ' AND title='MongoDB   '

MongoDB OR   
MongoDB OR            $or,      :
>db.col.find(
   {
      $or: [
	     {key1: value1}, {key2:value2}
      ]
   }
).pretty()

db.col.find({$or:[{"by" : "Runoob"},{"likes" : 99}]}).pretty()

AND   OR     
db.col.find({"likes": {$gt:11}, $or: [{"by": "Runoob"},{"title": "MongoDB-fyl"}]}).pretty()
  
where likes>11 and (by=Runoob or title=MongoDB-fyl)



db.col.find({likes : {$lt:200,$gt:100}}).pretty()


MongoDB Limit Skip  

db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP   " }
{ "title" : "Java   " }
db.col.find({},{"title":1}).limit(2)

db.col.find({},{"title":1}).limit(2).skip(1)



db.col.find({},{"title":3,_id:0,"by":1,_id:0}).limit(2)


MongoDB   
db.COLLECTION_NAME.ensureIndex({KEY:1})
    Key            ,1          ,               -1  。

MongoDB   
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
db.col.aggregate([{$group : {_id : "$likes", num_likes : {$sum : 1}}}])
      sql  : select likes, count(*) from col group by likes


db.col.aggregate([{$group : {_id : "$likes", num_likes : {$sum : "$likes"}}}])
      sql  : select likes, sum(likes) from col group by likes