我正试图在MongoDB中做这样的事情:
wiki_db
|
+-- Programming --+
|
|
+-- Perl -+
|
+-- tutorials --+
|
+-- 1 --+
|
| +---- id = 1
| |
+------+---- title = Introdaction To Perl
|
+---- Date = 12/11/2100
|
+---- Content = "Perl is .... "
这是一个维基应用程序的小型数据库我打算用perl编写它,其中:
programming = {
'perl': {
'tutorials': {
'1': {
'id':'1',
'title':'Hello World!',
'lastmod':'Sat Jul 23 14:56:22 AST 2011',
'Content': 'Hello!, This is the first page in the tutorial.'
}
}
}
}
问题在于,当我进行一些查询时,它什么都不返回:
$ mongo
MongoDB shell version: 1.8.2
connecting to: test
> use wiki
switched to db wiki
> show collections
programming
system.indexes
> db.programming.findOne()
{
"_id" : ObjectId("4e2f2fadce7012941395b103"),
"perl" : {
"tutorials" : {
"1" : {
"id" : "1",
"title" : "Hello World!",
"lastmod" : "Sat Jul 23 14:56:22 AST 2011",
"Content" : "Hello!, This is the first page in the tutorial."
}
}
}
}
> db.programming.find({"perl":{"tutorials":{"1":{"id":"1"}}}})
>
编写查询的正确方法是什么?树的设计好吗?我的意思是,当数据库长大时,该设计会减慢数据库的速度吗?
答案 0 :(得分:2)
这样做:
> db.programming.find({'perl.tutorials.1.id': '1'})
{ "_id" : ObjectId("4e2f45ef55bf6c17a7f511e0"), "perl" : { "tutorials" : { "1" : { "id" : "1", "title" : "Hello World" } } } }
答案 1 :(得分:1)
教程可以是一个数组:
programming = {
'perl': {
'tutorials': [
{
'id':'1',
'title':'Hello World!',
'lastmod':'Sat Jul 23 14:56:22 AST 2011',
'Content': 'Hello!, This is the first page in the tutorial.'
}
]
}
}
然后你可以这样做:
db.programming.find({'perl.tutorials.id': '1'})
然而,取决于每个主题有多少教程,以及它们有多长,这个模型可能会达到每个文档限制16MB。我建议将教程分成他们自己的集合,然后使用标签对它们进行精细处理:
db.tutorials.save({
"title" : "Hello World!",
"lastmod" : "Sat Jul 23 14:56:22 AST 2011",
"Content" : "Hello!, This is the first page in the tutorial.",
"Tags":['perl']
});
然后你可以找到这样的:
db.tutorials.find({Tags:'perl'});
这样可以防止达到16MB doc限制并使您的模型更灵活一些。 See Trees in MongoDB