Mongodb找到文件

时间:2016-09-01 08:42:29

标签: mongodb

我有一个包含文本翻译的MongoDB实例:

std::move(unique_ptr...)

我想做的是选择一个特定记录。例如,我希望通过这样做来选择最后一条记录:

{
        "_id" : ObjectId("57c68ba415f4d42b6ecd9ee7"),
        "en" : "Adana (pronounced [aˈda.na]) is a major city in southern Turkey. The city is situated on the Seyhan river, 35 km (22 mi) inland from the Mediterranean Sea, in south-central Anatolia. It is the administrative seat of the Adana Province and has a population of 1.7 million,[1] making it the fifth most populous city in Turkey. Adana-Mersin polycentric metropolitan area, with a population of 3 million, stretches over 70 km (43 mi) east-west and 25 km (16 mi) north-south; encompassing the cities of Mersin, Tarsus and Adana.",
        "sw" : "Adana (Kigiriki Άδανα) ni mji mkubwa katika nchi ya Uturuki. Kwa mujibu wa sensa iliyofanyika mwaka wa 2000, mji una wakazi wapatao 1,130,710 waishio huko,[2] na kuufanya kuwa mmoja kati ya miji mitano mikubwa ya Uturuku (baada ya Istanbul, Ankara, İzmir na Bursa). Mwaka wa 2006 mji wa Adana umekadiriwa kufikia iadadi ya wakazi wapatao 1,271,894. Huu ndiyo mji mkuu wa Mkoa wa Adana."
}
{
        "_id" : ObjectId("57c68ba915f4d42b6ecd9eea"),
        "en" : "Addis Ababa or Addis Abeba (the spelling used by the official Ethiopian Mapping Authority),(Amharic: አዲስ አበባ? Addis Abäba IPA: [adˈdis ˈabəba] ( listen), \"new flower\"; Oromo: Finfinne,[3][4] [fɪnˈfɪ́n.nɛ́] \"Natural Spring(s)\"), is the capital and largest city of Ethiopia. Finfinne is its Oromo name. It has a population of 3,384,569 according to the 2007 population census, with annual growth rate of 3.8%. This number has been increased from the originally published 2,738,248 figure and appears to be still largely underestimated.[2][5]",
        "sw" : "Addis Ababa (pia Addis Abeba; kwa Kiamhara አዲስ አበባ, \"Ua Jipya\"; kwa Kioromo Finfinne) ni mji mkuu wa Ethiopia na wa Umoja wa Afrika."
}
{
        "_id" : ObjectId("57c68bab15f4d42b6ecd9eec"),
        "en" : "Adelaide of Italy (931 – 16 December 999), also called Adelaide of Burgundy, was the second wife of Holy Roman Emperor Otto the Great[2] and was crowned as the Holy Roman Empress with him by Pope John XII in Rome on February 2, 962. Empress Adelaide was perhaps the most prominent European woman of the 10th century; she was regent of the Holy Roman Empire as the guardian of her grandson in 991-995.[2]",
        "sw" : "Adelaide wa Italia (takriban 931 – 16 Desemba, 999) alikuwa binti wa Rudolf II, mfalme wa Burgundia. Kwanza aliolewa na Lothar, mfalme wa Italia. Alipofariki Lothar, Adelaide aliolewa na Otto I, mfalme wa Ujerumani. Aliishi maisha matakatifu. Sikukuu yake ni 16 Desemba."
}

但是mongo shell什么也没有返回。

事实上,我知道我可以创建一个索引,并执行以下操作:

db.wiki.find({"sw": "Adelaide wa Italia"}).pretty();

确实按预期返回记录。

我在非索引搜索中做错了什么?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该使用regex搜索:

db.wiki.find({"sw": /Adelaide wa Italia/}).pretty();

你的方式是:

db.wiki.find({"sw": "Adelaide wa Italia"}).pretty();

您只是告诉Mongo将sw等于Adelaide wa Italia的所有文档都退回给您,但您希望在sw字段中获取包含此词组的所有文档。