一个简单的请求Mongo DB导致”分段错误“

我决定尝试用lua编写一个脚本。我去了文档并写了下面的代码。

local argparse = require "argparse"
local inspect = require('inspect')
local mongo = require 'mongo'

local parser = argparse("script", "Clear DB and seed init data")
parser:argument("input", "Input file.")
parser:option("-m --mongouri", "mongo connection uri", "mongodb://localhost:27017")

local args = parser:parse()
print(inspect(args))

local client = mongo.Client(args.mongouri)
local collection = client:getCollection('Messages', 'messages')

collection:drop() -- 清空集合

-- 常规变量
collection:insert{_id = mongo.ObjectID(), name = 'John Smith', age = 50}
collection:insert{_id = mongo.ObjectID(), name = 'Mukul Latiyan', age = 24}
collection:insert{_id = mongo.ObjectID(), name = 'Rahul', age = 32}

-- 迭代文档
for person in collection:find({}, {sort = {age = -1}}):iterator() do
    print(person.name, person.age)
end

连接成功,文档被插入到指定的集合中。但在获取时出现了类型错误:

[1] 38330 segmentation fault lua ./scripts/init_dbs.lua -m test.out

从文档中复制的简单代码导致了困难,告诉我我做错了什么。

原文链接 https://stackoverflow.com/questions/69434650

点赞