モンゴDB初日
11410 ワード
もっと読む
最近は大丈夫だと思いますが、mongoDBは牛のように強いです.彼は通常の関係型データベースほど多くの制約がないので、文法はjsと同じように見えます.
最近は大丈夫だと思いますが、mongoDBは牛のように強いです.彼は通常の関係型データベースほど多くの制約がないので、文法はjsと同じように見えます.
mongod --dbpath C:\zhuyang\zhuyang\mongodb\mongodb\db
---insert
db.person.insert({"name":"zhuyang","sex":"male")
db.person.insert({"name":"hello","sex":"world"})
person = {"name":"nametest","sex":"sextest"}
db.person.insert(person)
---find
db.person.find()
db.person.find({"name":"nametest"})
db.person.findOne()
--update
db.person.update({"name":"nametest"},{"sex":"testupdate"})
--remove
db.person.remove({"name":"nametest"})
db.person.remove({"sex":"testupdate"})
--------------some command
help ----see help doc
db.help(); --to see some command of db level
db.foo.help() ----help of collections
to see the funcion implementation can use function name directly. eg: db.foo.update
---------insert to doc -------------
--insert one record
db.foo.insert({"firstname":"zhu"}) --after this sentence , db will add another _id key(if doesnt defined) and save to doc.see below
db.foo.insert({"lastname":"yang"})
db.foo.insert({"firstname":"chen"})
> db.foo.find()
{ "_id" : ObjectId("52ce585fe2e2fd0a66329ee3"), "firstname" : "zhu" }
------------batch insert
use mongoimport
file<4MB, need a field name _id
-----------remove doc
db.foo.remove() ---remove all
db.foo.remove({"firstname":"chen"}) ---remove one record
------update doc ------
see below
db.user.insert(
{
"firstname":"zhu",
"lastname":"yang",
"friends":32,
"enemies":2
}
)
CHANGE TO
{
"username":"zhuyang",
"relation":
{
"friends":32,
"enemies":2
}
}
var zhuyang = db.user.findOne({"firstname":"zhu"})
zhuyang.relation={
"friends":zhuyang.friends,
"enemies":zhuyang.enemies
}
zhuyang.username="zhuyang"
delete zhuyang.firstname
delete zhuyang.lastname
delete zhuyang.friends
delete zhuyang.enemies
db.user.update({"firstname":"zhu"},zhuyang)
$set
db.user.insert(
{
"name":"joe",
"age":30,
"sex":"male",
"location":"shanghai"
}
)
// favorite book
// 2
//1. update()
var joe = db.user.findOne({"name":"joe"})
joe.favoritebook="java";
db.user.update({"name":"joe"},joe);
//2. $set
db.user.update(
{
"name":"joe"
},
{
"$set":{
"favoritebook":"c++"
}
}
)
// favorite book
db.user.update(
{
"name":"joe"
},
{
"$set":{
"favoritebook":[
"c++","c##","perl"
]
}
}
)
// $unset
db.user.update(
{
"name":"joe"
},
{
"$unset":{
"favoritebook":1
}
}
)
// $set
db.blog.posts.insert({
"title":"java test",
"content":"this is content",
"author":{
"name":"sb",
"mail":"[email protected]"
}
}
)
// joe zhu
db.blog.posts.update(
{
"author.name":"sb"
},
{
"$set":
{
"author.name":"joe zhu"
}
}
)
//$inc ,
db.games.insert(
{
"game":"pinball",
"user":"joe"
}
)
// , . , 50. $inc 50
db.games.update(
{
"game":"pinball",
"user":"joe"
},
{
"$inc":{
"score":50
}
}
)
// , $inc score , 50
// , 1000 .
db.games.update(
{
"game":"pinball",
"user":"joe"
},
{
"$inc":{
"score":1000
}
}
)
// 50+1000=1050
//
// , $push , .
db.blog.posts.findOne({"author.mail":"[email protected]"});
{
"_id" : ObjectId("52cf9f70cae129f46ff6fa5d"),
"author" : {
"mail" : "[email protected]",
"name" : "joe zhu"
},
"content" : "this is content",
"title" : "java test"
}
// comments . :
db.blog.posts.update(
{
"author.mail":"[email protected]"
},
{
"$push":{
comments:{
"name":"zhu",
"email":"[email protected]",
"content":"nice one"
}
}
}
);
> db.blog.posts.find()
{ "_id" : ObjectId("52cf9f70cae129f46ff6fa5d"), "author" : { "mail" : "[email protected]", "name" : "joe zhu" }, "comments" : [ { "name" : "zhu", "email" : "[email protected]",
"content" : "nice one" } ], "content" : "this is content", "title" : "java test" }
//
db.blog.posts.update(
{
"author.mail":"[email protected]"
},
{
"$push":{
"comments":{
"name":"yang",
"email":"[email protected]",
"content":"nice two"
}
}
}
)
db.blog.posts.findOne()
"_id" : ObjectId("52cf9f70cae129f46ff6fa5d"),
"author" : {
"mail" : "[email protected]",
"name" : "joe zhu"
},
"comments" : [
{
"name" : "zhu",
"email" : "[email protected]",
"content" : "nice one"
},
{
"name" : "yang",
"email" : "[email protected]",
"content" : "nice two"
}
],
"content" : "this is content",
"title" : "java test"
$addToSet
db.blog.posts.update(
{
"author.mail":"[email protected]"
},
{
"$addToSet":{
"comments":{
"name":"yang1",
"email":"[email protected]",
"content":"nice 3"
}
}
}
)
$addToSet $each
db.user.update(
{
"_id":ObjectId("52cf9c6dcae129f46ff6fa5c")
},
{
"$addToSet":{
"favoritebook":{
"$each":[
"javascript",
"mongo in action"
]
}
}
}
)
// , . $pop , {$pop:{key:1}} 1 ,{$pop:{key:-1}} 1
db.lists.insert(
{
"todo":[
"dishes",
"laundry",
"dry cleaning"
]
}
)
db.lists.update(
{
"_id":ObjectId("52cfac11cae129f46ff6fa5f")
},
{
"$pop":{
"todo":-1
}
}
)
// , ,"$pull"
db.lists.update(
{
"_id":ObjectId("52cfad13cae129f46ff6fa60")
},
{
"$pull":{
"todo":"laundry"
}
}
)
//
// , . $
// 0 ,
> db.blog.posts.findOne()
{
"_id" : ObjectId("52cfae34cae129f46ff6fa61"),
"author" : {
"name" : "sb",
"mail" : "[email protected]"
},
"comments" : [
{
"name" : "yang",
"email" : "[email protected]",
"content" : "nice two",
"vote" : 5
},
{
"name" : "yang1",
"email" : "[email protected]",
"content" : "nice 1",
"vote" : 6
}
],
"content" : "this is content",
"title" : "java test"
}
//
db.blog.posts.update(
{
"_id": ObjectId("52cfae34cae129f46ff6fa61")
},
{
"$inc":{// $inc , vote 100
"comments.0.vote":100//comments.0 1
}
}
)
// , , $
db.blog.posts.update(
{
"comments.name": "yang" // ,
},
{
"$set":{//
"comments.$.vote":300
}
}
)
//upsert , ,
db.user.update(
{
"edu":"gaozhong"// edu==gaozhong
},
{
"$set":{
"age":100
}
},
true//true upsert
)
db.user.find()
{ "_id" : ObjectId("52cfb32dedd18a0edc7db7b0"), "age" : 100, "edu" : "gaozhong" }