From 84ee3da79a5c5e18d58d83c5db841497b43576c1 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 11:55:31 +0800 Subject: [PATCH 1/5] add mongo.createIndexs, and fix the bug in old API createIndex. For detail, see PR #511 --- lualib/mongo.lua | 67 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index a1a2e11e..e6ccc5e8 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -318,30 +318,63 @@ function mongo_cursor:count(with_limit_and_skip) end +-- For compatibility. -- collection:createIndex({username = 1}, {unique = true}) -function mongo_collection:createIndex(keys, option) - local name = option.name - option.name = nil - - if not name then - for k, v in pairs(keys) do - name = (name == nil) and k or (name .. "_" .. k) - name = name .. "_" .. v - end - end - - - local doc = {}; - doc.name = name - doc.key = keys - for k, v in pairs(option) do +local function createIndex_onekey(self, key, option) + local doc = {} + for k,v in pairs(option) do doc[k] = v end + local k,v = next(key) -- support only one key + doc.name = doc.name or (k .. "_" .. v) + doc.key = key + return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end -mongo_collection.ensureIndex = mongo_collection.createIndex; +local function IndexModel(option) + local doc = {} + for k,v in pairs(option) do + if type(k) == "string" then + doc[k] = v + end + end + + local keys = {} + local name + for _, kv in ipairs(option) do + local k,v = next(kv) + table.insert(keys, k) + table.insert(keys, v) + name = (name == nil) and k or (name .. "_" .. k) + name = name .. "_" .. v + end + + doc.name = doc.name or name + doc.key = bson_encode_order(table.unpack(keys)) + + return doc +end + +-- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } +function mongo_collection:createIndex(option , onekey) + if onekey then + return createIndex_onekey(self, option, onekey) + end + + return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) +end + +function mongo_collection:createIndexs(...) + local idx = { ... } + for k,v in ipairs(idx) do + idx[k] = IndexModel(v) + end + return self.database:runCommand("createIndexes", self.name, "indexes", idx) +end + +mongo_collection.ensureIndex = mongo_collection.createIndex function mongo_collection:drop() return self.database:runCommand("drop", self.name) From ee1a21887015761b13cf2163d55711cd33384ab0 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:01:54 +0800 Subject: [PATCH 2/5] typo --- lualib/mongo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index e6ccc5e8..977d4c3a 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -366,7 +366,7 @@ function mongo_collection:createIndex(option , onekey) return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) end -function mongo_collection:createIndexs(...) +function mongo_collection:createIndexes(...) local idx = { ... } for k,v in ipairs(idx) do idx[k] = IndexModel(v) From bd3d9b70ec297c30985a1c22e17c51329ca16429 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:05:52 +0800 Subject: [PATCH 3/5] change arg name --- lualib/mongo.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 977d4c3a..c33c8d6c 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -358,12 +358,12 @@ local function IndexModel(option) end -- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } -function mongo_collection:createIndex(option , onekey) - if onekey then - return createIndex_onekey(self, option, onekey) +function mongo_collection:createIndex(arg1 , arg2) + if arg2 then + return createIndex_onekey(self, arg1, arg2) + else + return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(arg1) }) end - - return self.database:runCommand("createIndexes", self.name, "indexes", { IndexModel(option) }) end function mongo_collection:createIndexes(...) From fd2c814fe04c8b76a1c53bd7a718b39a475fd071 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:13:02 +0800 Subject: [PATCH 4/5] default key ascending --- lualib/mongo.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index c33c8d6c..4806bee0 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -344,7 +344,13 @@ local function IndexModel(option) local keys = {} local name for _, kv in ipairs(option) do - local k,v = next(kv) + local k,v + if type(kv) == "string" then + k = kv + v = 1 + else + k,v = next(kv) + end table.insert(keys, k) table.insert(keys, v) name = (name == nil) and k or (name .. "_" .. k) @@ -358,6 +364,8 @@ local function IndexModel(option) end -- collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } +-- or collection:createIndex { "key1", "key2", unique = true } +-- or collection:createIndex( { key1 = 1} , { unique = true } ) -- For compatibility function mongo_collection:createIndex(arg1 , arg2) if arg2 then return createIndex_onekey(self, arg1, arg2) From bb0143e476866d733d138311eb37743b9f0bd7cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 13 Jun 2016 12:28:13 +0800 Subject: [PATCH 5/5] add some assert --- lualib/mongo.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 4806bee0..895adb62 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -326,6 +326,7 @@ local function createIndex_onekey(self, key, option) doc[k] = v end local k,v = next(key) -- support only one key + assert(next(key,k) == nil, "Use new api for multi-keys") doc.name = doc.name or (k .. "_" .. v) doc.key = key @@ -356,6 +357,7 @@ local function IndexModel(option) name = (name == nil) and k or (name .. "_" .. k) name = name .. "_" .. v end + assert(name, "Need keys") doc.name = doc.name or name doc.key = bson_encode_order(table.unpack(keys))