针对简单python应用程序的传统SQL与MongoDB/CouchDB
假设我得到了这样一个传统的SQL结构:针对简单python应用程序的传统SQL与MongoDB/CouchDB,python,sql,mongodb,couchdb,Python,Sql,Mongodb,Couchdb,假设我得到了这样一个传统的SQL结构: create table tags (id PRIMARY KEY int, tag varchar(100)); create table files (id PRIMARY KEY int, filename varchar(500)); create table tagged_files (tag_id int, file_id int); 我添加了一些标签: insert into table tags (tag) values ('places
create table tags (id PRIMARY KEY int, tag varchar(100));
create table files (id PRIMARY KEY int, filename varchar(500));
create table tagged_files (tag_id int, file_id int);
我添加了一些标签:
insert into table tags (tag) values ('places');
insert into table tags (tag) values ('locations');
和一些文件:
insert into table files (filename) values ('/tmp/somefile');
insert into table files (filename) values ('/tmp/someotherfile');
然后标记这些文件:
insert into table tagged_files (tag_id, file_id) values (1,1);
insert into table tagged_files (tag_id, file_id) values (1,2);
然后我可以找到所有用第一个标记标记的文件,如下所示:
select * from files, tagged_files where id = file_id and tag_id = 1
但是,我如何使用NoSQL解决方案(如MongoDB和CouchDB)做同样的事情呢??什么样的NoSQL项目最适合这种情况呢?对于MongoDB
您可能只需将它们与标记一起保存为:
db.Files.save({ "fn" : "/tmp/somefile", "ts" : [ { "t" : "places" }, { "t" : "locations" }] });
db.Files.save({ "fn" : "/tmp/someotherfile", "ts" : [ { "t" : "locations" }] });
另一种方法是单独保存标记(ObjectId为12字节iirc):
在这两种情况下,获取文件
db.Files.find({ "_id" : ObjectId("4c19e79e244f000000007e0d") });
db.Files.find({ "fn" : "/tmp/somefile" });
或基于标记的两个文件:
db.Files.find({ ts : { t : "locations" } })
db.Files.find({ t : { i : ObjectId("4c19e79e244f000000007e0d") } })
示例来自mongo控制台。如果你存储了Id为的标签,那么一旦你得到了文件,你就必须根据Id来读取标签
db.Files.find({ ts : { t : "locations" } })
db.Files.find({ t : { i : ObjectId("4c19e79e244f000000007e0d") } })