2017-10-16 106 views
0

我正在使用d語言進行編碼,並試圖提取最後一個從文檔的陣列修改的文檔,與d語言(在控制檯不!!!!) 該查詢是這樣的:我如何在d-languge中找到mongodb集合中的最後一個修改數據,並用find({「...」:「..」})進行篩選

Collection ct = mongo.web.cell; 
auto cell = ct.find({"room": 4 }).sort({'_id': -1 }).limit(1); 

和通過配音給出的誤差是

source/app.d(166,58): Error: found : when expecting ; following statement 
source/app.d(166,61): Error: found } when expecting ; following statement 
source/app.d(166,62): Error: found) instead of statement 

當更改到達單元格數據插入的房間,未更新 我正在使用Visual Studio代碼,最新版本

有什麼想法?

+0

'{「房間」:4}'是沒有有效的語法,如果你想通過你需要使用'關聯數組[「房間」:4]' – weltensturm

+0

THKS但沒't不是工作,而是得到這些錯誤,而不是: source/app.d(170,54):錯誤:未終止的字符常量 source/app.d(170,56):錯誤:在期望時發現ID;以下語句 source/app.d(170,58):錯誤:未終止的字符常量 source/app.d(170,60):Error:found:when expected;以下語句 source/app.d(170,63):Error:found} when expected;下面的語句 source/app.d(170,64):Error:found)而不是語句 –

+0

而且如果我將行簡化爲find([「room」:4]);我得到這個錯誤mondo.Collection.find(T = BsonObject)(在查詢query = Query.init中,在QueryFlags標誌= QueryFlags.NONE中,在ReadPrefs readPrefs = null中) –

回答

2

我推斷你使用mondo,如果你看過庫的文檔和一些來自herehere的代碼,以下是你需要做什麼:

Mongo mongo = new Mongo("mongodb://yourhost"); 
Collection ct = mongo.web.cell; 

auto q = new Query(); // create a new query object 
q.conditions["room"] = 4; // specify the query condition 

auto s = new BsonObject("_id", "-1"); // creat a new bson object 
q.sorts(s.dup); // use sorts not sort 

ct.find(q).each!writeln; // find the results 

當您使用排序,你會使用std.algorithm.sorting而不是mondo的排序功能。

希望工程

+0

嗨Quarashi它似乎一直工作到打印,它開始抱怨(運行3.0.6 mongodb):錯誤:沒有屬性'每個'類型'光標!(BsonObject)' –

+0

從手冊爲3.0: db .users.find()。forEach(function(myDoc){print(「user:」+ myDoc.name);}); 也不管用 我知道我得到了mondo.collection –

+0

@AndersS,'each'是一個標準的庫迭代函數,只需導入'std.algorithm'並且你可以使用它,或者你可以返回結果然後使用foreach。 – Qurashi

相關問題