mongodb固定集合及gridfs

发布时间:2021-07-16 17:15:30 阅读:1272次
root@test:/home/test/mongodb/bin# mongo -uroot -p123456 admin
MongoDB shell version: 1.8.1
connecting to: admin
> use siyuroom
switched to db siyuroom
> use siyuroom
switched to db siyuroom
> show collections
detailinfo
siyuroom_log
system.indexes
system.users
userinfo
v9_picture_favorite
 
> show dbs
admin 0.0625GB
local 0.0625GB
siyuroom 1.49951171875GB
test 0.0625GB
> use test
switched to db test
> show tables;
system.indexes
system.users
> db            
test
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
> use test
switched to db test
> show tables;
> db
test
> db.c1.insert({"username":"1"}) 
> show tables;
c1
system.indexes
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> use test
switched to db test
> db.createCollection("c1")                                               
{ "ok" : 1 }
> db.createCollection("c2")
{ "ok" : 1 }
> show tables;
c1
c2
system.indexes
> db.c1.drop();
true
> db.c2.drop();
true
> show tables;
system.indexes
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
> show tables;
> db.createCollection("c1")
{ "ok" : 1 }
> show tables;
c1
system.indexes
> db.c1.stats();
{
"ns" : "test.c1",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8192,
"indexSizes" : {
"_id_" : 8192
},
"ok" : 1
}
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "test.c1", "key" : { "_id" : 1 }, "v" : 0 }
> db.c1.stats();
{
"ns" : "test.c1",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8192,
"indexSizes" : {
"_id_" : 8192
},
"ok" : 1
}
> db.createCollection("c2",{capped:true,size:1000000,max:5});
{ "ok" : 1 }
> show tables;
c1
c2
system.indexes
> db.c2.stats();
{
"ns" : "test.c2",
"count" : 0,
"size" : 0,
"storageSize" : 1000192,
"numExtents" : 1,
"nindexes" : 0,
"lastExtentSize" : 1000192,
"paddingFactor" : 1,
"flags" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"capped" : 1,
"max" : 5,
"ok" : 1
}
> db.c2.insert({"username":"user1"}); 
> db.c2.insert({"username":"user2"});
> db.c2.insert({"username":"user3"});
> db.c2.insert({"username":"user4"});
> db.c2.insert({"username":"user5"});
> db.c2.find();                      
{ "_id" : ObjectId("5288b76d022d86f30b9155f2"), "username" : "user1" }
{ "_id" : ObjectId("5288b76f022d86f30b9155f3"), "username" : "user2" }
{ "_id" : ObjectId("5288b771022d86f30b9155f4"), "username" : "user3" }
{ "_id" : ObjectId("5288b774022d86f30b9155f5"), "username" : "user4" }
{ "_id" : ObjectId("5288b776022d86f30b9155f6"), "username" : "user5" }
> db.c2.insert({"username":"user6"});
> db.c2.find();                      
{ "_id" : ObjectId("5288b76f022d86f30b9155f3"), "username" : "user2" }
{ "_id" : ObjectId("5288b771022d86f30b9155f4"), "username" : "user3" }
{ "_id" : ObjectId("5288b774022d86f30b9155f5"), "username" : "user4" }
{ "_id" : ObjectId("5288b776022d86f30b9155f6"), "username" : "user5" }
{ "_id" : ObjectId("5288b780022d86f30b9155f7"), "username" : "user6" }
> db.c2.insert({"username":"user7"});
> db.c2.find();                      
{ "_id" : ObjectId("5288b771022d86f30b9155f4"), "username" : "user3" }
{ "_id" : ObjectId("5288b774022d86f30b9155f5"), "username" : "user4" }
{ "_id" : ObjectId("5288b776022d86f30b9155f6"), "username" : "user5" }
{ "_id" : ObjectId("5288b780022d86f30b9155f7"), "username" : "user6" }
{ "_id" : ObjectId("5288b787022d86f30b9155f8"), "username" : "user7" }
> db.c2.stats();
{
"ns" : "test.c2",
"count" : 5,
"size" : 220,
"avgObjSize" : 44,
"storageSize" : 1000192,
"numExtents" : 1,
"nindexes" : 0,
"lastExtentSize" : 1000192,
"paddingFactor" : 1,
"flags" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"capped" : 1,
"max" : 5,
"ok" : 1
}
> db.c1.stats();                                                          
{
"ns" : "test.c1",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8192,
"indexSizes" : {
"_id_" : 8192
},
"ok" : 1
}
> db.runCommand({"convertToCapped":"c1",size:1000000})
{ "ok" : 1 }
> db.c1.stats();                                      
{
"ns" : "test.c1",
"count" : 0,
"size" : 0,
"storageSize" : 1000192,
"numExtents" : 1,
"nindexes" : 0,
"lastExtentSize" : 1000192,
"paddingFactor" : 1,
"flags" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"capped" : 1,
"max" : 2147483647,
"ok" : 1
}
 
 
gridFS
root@test:~# mongofiles put foo.txt 
connected to: 127.0.0.1
added file: { _id: ObjectId('5288bf0c9486bd0c5c6a682c'), filename: "foo.txt", chunkSize: 262144, uploadDate: new Date(1384693516472), md5: "e68af1c984c6bad72e49aca6d0a3e907", length: 12 }
done!
root@test:~# mongofiles put phpfile.tar.gz 
connected to: 127.0.0.1
added file: { _id: ObjectId('5288bf8cb372fa2afb5f4b93'), filename: "phpfile.tar.gz", chunkSize: 262144, uploadDate: new Date(1384693644993), md5: "471a5f0965eebc790f8c1c400d926c5b", length: 125 }
done!
root@test:~# mongofiles list
connected to: 127.0.0.1
foo.txt 12
phpfile.tar.gz 125
root@test:~# mongo
MongoDB shell version: 1.8.1
connecting to: test
> show tables;
c1
c2
fs.chunks
fs.files
system.indexes
> db.fs.chunks.find()
{ "_id" : ObjectId("5288bf0c854db929d754ebe1"), "files_id" : ObjectId("5288bf0c9486bd0c5c6a682c"), "n" : 0, "data" : BinData(0,"aGVsbG8ud29ybGQK") }
{ "_id" : ObjectId("5288bf8c854db929d754ebe2"), "files_id" : ObjectId("5288bf8cb372fa2afb5f4b93"), "n" : 0, "data" : BinData(0,"H4sIADy7iFIAA+3SOwqAQAyE4dSeYk8gyRr3PhaChbDi4/4ulhaKhYjwf80UmWKKWD0Nk7xLi+R+ZHFOVYtiMXq0pvVkoqZuSYK+vOuwLWs3hyBzzutV7+7+U0M/jjmUH6i+XgIAAAAAAAAAAAAAAAAAeGIHBs8iOQAoAAA=") }
> db.fs.files.find()
{ "_id" : ObjectId("5288bf0c9486bd0c5c6a682c"), "filename" : "foo.txt", "chunkSize" : 262144, "uploadDate" : ISODate("2013-11-17T13:05:16.472Z"), "md5" : "e68af1c984c6bad72e49aca6d0a3e907", "length" : 12 }
{ "_id" : ObjectId("5288bf8cb372fa2afb5f4b93"), "filename" : "phpfile.tar.gz", "chunkSize" : 262144, "uploadDate" : ISODate("2013-11-17T13:07:24.993Z"), "md5" : "471a5f0965eebc790f8c1c400d926c5b", "length" : 125 }
> exit
bye
root@test:~# ls
1.php        ip_forwarx~  mbox                qt
GNUstep      ip_forwary~  overcommit_memory~  sent
ip_forward~  ip_forwarz~  phpfile.tar.gz      v9_news_bi_content.sql
ip_forwarw~  Mail         postponed
root@test:~# rm -rf 1.php
root@test:~# rm -rf phpfile.tar.gz 
root@test:~# mongofiles list
connected to: 127.0.0.1
foo.txt 12
phpfile.tar.gz 125
root@test:~# mongofiles get foo.txt
connected to: 127.0.0.1
done write to: foo.txt
root@test:~# ls -rlht
-rw-r--r-- 1 root root   12 11月 17 21:08 foo.txt
root@test:~# mongofiles get phpfile.tar.gz
connected to: 127.0.0.1
done write to: phpfile.tar.gz
root@test:~# mongofiles list
connected to: 127.0.0.1
foo.txt 12
phpfile.tar.gz 125
root@test:~# mongofiles delete foo.txt
connected to: 127.0.0.1
done!
root@test:~# mongofiles list
connected to: 127.0.0.1
phpfile.tar.gz 125
root@test:~# mongofiles delete phpfile.tar.gz
connected to: 127.0.0.1
done!
root@test:~# mongofiles list
connected to: 127.0.0.1
root@test:~#

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:mongodb固定集合及gridfs 出自老鄢博客 | 欢迎分享