neoxic / lua-mongo

MongoDB Driver for Lua
MIT License
141 stars 38 forks source link

unable to make an index creation #4

Closed FBRISSON closed 6 years ago

FBRISSON commented 6 years ago

I would like to create an index for my database but the syntax 👍 :ensureIndex(.. seems to be not implemented.

neoxic commented 6 years ago

Index creation functions in Mongo C Driver are deprecated. See mongoc_collection_create_index() for example.

To create indexes on a MongoDB collection, your best bet is to execute the createIndexes command on a MongoDB server via client:command(). For example:

client:command [[
{
    createIndexes: <collection>,
    indexes: [
        {
            key: {
                <key-value_pair>,
                <key-value_pair>,
                ...
            },
            name: <index_name>,
            <option1>,
            <option2>,
            ...
        },
        { ... },
        { ... }
    ],
}
]]

See createIndexes for more information.

FBRISSON commented 6 years ago

Thank for your prompt reply, I really appreciate After 2 hours of digging I finally get it working in LUA and find the write syntax

nbclient=OP_FindClient("ClientName")
client=op_client[nbclient]  -- get the client among multiple ones
query = [[
    {
        "createIndexes":"sites",
        "indexes" : [
            {
            "key" : {
                "name":1,
                "addr":1,
                "ville":1
                },
                "name":"idx"
            }
            ]
    }
    ]]
queryBSON = mongo.BSON(query)
ret = client:command("myDBname",queryBSON)