如何使用mongoose查询mongoDB?

时间:2022-01-01 11:59:05

How do you query MongoDB using mongoose with node.js? I have it to where I can insert my JSON object to my DB, but all the ways I have tried to return the JSON object from the DB return null or just information about the database.

你如何使用mongoose与node.js查询MongoDB?我有它可以将我的JSON对象插入到我的数据库中,但是我尝试从数据库返回JSON对象的所有方法都返回null或只是有关数据库的信息。

Is there some good method using mongoose to be able to query the database similar to the method:

有没有一些很好的方法使用mongoose能够查询类似于该方法的数据库:

var cursor = db.collection.find()
var JSONobject = cursor.next()

Here's what is in my code right now:

这是我现在的代码:

mongoose.connect('mongodb://localhost/myDB');
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
var cursor = mongoose.connection.db.contents.find();
console.log(cursor.next());

This throws an error at the line :

这会在该行引发错误:

var cursor = mongoose....

claiming 'cannot call method 'find' of undefined'.

声称'不能调用方法'找到'未定义'。

Note, that my collection 'contents' does in fact exist, and it contains one JSON document. I know this because I manually navigated to the collection using the mongo shell.

请注意,我的集合“内容”确实存在,并且它包含一个JSON文档。我知道这是因为我使用mongo shell手动导航到集合。

Edit: I am open to alternative methods to querying the database. I simply just want to be able to return JSON objects from my DB one at a time, while keeping track of where the client is at in the database.

编辑:我愿意接受查询数据库的替代方法。我只是希望能够一次从我的数据库中返回JSON对象,同时跟踪客户端在数据库中的位置。

1 个解决方案

#1


One method to query mongoDB using mongoose is as follows:

使用mongoose查询mongoDB的一种方法如下:

Content.findOne().exec(function(err,docs){console.log(docs)});

Docs contains the JSON object. Access its attributes like any other object.

文档包含JSON对象。像任何其他对象一样访问其属性。

Note, this method uses asynchronous call backs. As such, you can't store the JSON object docs to a variable in order to use the JSON document information outside of the function. Therefore, you need to perform whatever actions needed with the JSON docs object information inside of the function.

注意,此方法使用异步回调。因此,您无法将JSON对象文档存储到变量中,以便在函数外部使用JSON文档信息。因此,您需要执行函数内部的JSON docs对象信息所需的任何操作。

For example, I was needing the query information to provide the filepath for my get api. As such, the result was as follows:

例如,我需要查询信息来为我的get api提供文件路径。因此,结果如下:

//get
app.get('/api/media/', function(req,res){
    Content.findOne().exec(function(err,docs){res.sendFile(path.join(__dirname, '/api/media/', docs.filename))});

});

Note, Content is the model of my schema, and one of its parameters is filename.

注意,Content是我的模式的模型,其中一个参数是filename。

#1


One method to query mongoDB using mongoose is as follows:

使用mongoose查询mongoDB的一种方法如下:

Content.findOne().exec(function(err,docs){console.log(docs)});

Docs contains the JSON object. Access its attributes like any other object.

文档包含JSON对象。像任何其他对象一样访问其属性。

Note, this method uses asynchronous call backs. As such, you can't store the JSON object docs to a variable in order to use the JSON document information outside of the function. Therefore, you need to perform whatever actions needed with the JSON docs object information inside of the function.

注意,此方法使用异步回调。因此,您无法将JSON对象文档存储到变量中,以便在函数外部使用JSON文档信息。因此,您需要执行函数内部的JSON docs对象信息所需的任何操作。

For example, I was needing the query information to provide the filepath for my get api. As such, the result was as follows:

例如,我需要查询信息来为我的get api提供文件路径。因此,结果如下:

//get
app.get('/api/media/', function(req,res){
    Content.findOne().exec(function(err,docs){res.sendFile(path.join(__dirname, '/api/media/', docs.filename))});

});

Note, Content is the model of my schema, and one of its parameters is filename.

注意,Content是我的模式的模型,其中一个参数是filename。