通过 nodejs 查询 mongodb 显示空结果?
Querying mongodb via nodejs shows empty result?
1.I 我正在尝试通过节点 below.js 在数据库 "teamautomation" 中查询集合 "radar_life_cycle",如下所示
2.I创建了对应的模型
,通过 mongoose 连接到 mongodb 成功,没有错误,但我得到一个空列表。
- 正如您从下面帖子的输出中看到的,
[]
是一个空列表,但我可以看到数据库中有记录,任何人都可以提供有关这里可能出现的问题的指导吗?
app.js
const express = require("express");
const mongoose = require("mongoose");
const Radar_life_cycle = require("./models/radar_life_cycle");
const app = express();
mongoose
.connect(
"mongodb://username:password@1x.xxx.xxx.x:27017/wifiautomation"
)
.then(() => {
console.log("Connected to database!");
})
.catch(() => {
console.log("Connection failed!");
});
app.get("/api/radars", (req, res, next) => {
Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
models/radar_life_cycle
const mongoose = require('mongoose');
const radar_life_cycle_Schema = mongoose.Schema({
Delivered: String,
orgRadar: String,
root_build: String,
inserted_by: String,
milestone: String,
applicable_chipsets: [String],
project_tag: String,
gerrits:String,
inserted_on:Date,
SDK:String
});
module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);
输出:-
terminal$ nodemon server.js
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
(node:17500) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected to database!
{"message":"Posts fetched successfully!","posts":[]}
预期输出:-
{"message":"Posts fetched successfully!","posts":should include the corresponding records}
数据库文件截图:
app.get("/api/radars", (req, res, next) => {
Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents[0]
});
});
});
find
方法在这种情况下查找数据库中的所有文档,似乎只有一个...如果有更多,您可以更新上面的代码并使用循环访问文档。
以这种方式尝试架构可能无法很好地引用集合
const { Schema } = require('mongoose');
const radar_life_cycle_Schema= new Schema({
Delivered: String,
orgRadar: String,
root_build: String,
inserted_by: String,
milestone: String,
applicable_chipsets: [String],
project_tag: String,
gerrits:String,
inserted_on:Date,
SDK:String
},
{
collection: 'radar_life_cycle',
timestamps: { createdAt: true, updatedAt: true },
});
module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);
1.I 我正在尝试通过节点 below.js 在数据库 "teamautomation" 中查询集合 "radar_life_cycle",如下所示
2.I创建了对应的模型 ,通过 mongoose 连接到 mongodb 成功,没有错误,但我得到一个空列表。
- 正如您从下面帖子的输出中看到的,
[]
是一个空列表,但我可以看到数据库中有记录,任何人都可以提供有关这里可能出现的问题的指导吗?
app.js
const express = require("express");
const mongoose = require("mongoose");
const Radar_life_cycle = require("./models/radar_life_cycle");
const app = express();
mongoose
.connect(
"mongodb://username:password@1x.xxx.xxx.x:27017/wifiautomation"
)
.then(() => {
console.log("Connected to database!");
})
.catch(() => {
console.log("Connection failed!");
});
app.get("/api/radars", (req, res, next) => {
Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
models/radar_life_cycle
const mongoose = require('mongoose');
const radar_life_cycle_Schema = mongoose.Schema({
Delivered: String,
orgRadar: String,
root_build: String,
inserted_by: String,
milestone: String,
applicable_chipsets: [String],
project_tag: String,
gerrits:String,
inserted_on:Date,
SDK:String
});
module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);
输出:-
terminal$ nodemon server.js
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
(node:17500) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected to database!
{"message":"Posts fetched successfully!","posts":[]}
预期输出:-
{"message":"Posts fetched successfully!","posts":should include the corresponding records}
数据库文件截图:
app.get("/api/radars", (req, res, next) => {
Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents[0]
});
});
});
find
方法在这种情况下查找数据库中的所有文档,似乎只有一个...如果有更多,您可以更新上面的代码并使用循环访问文档。
以这种方式尝试架构可能无法很好地引用集合
const { Schema } = require('mongoose');
const radar_life_cycle_Schema= new Schema({
Delivered: String,
orgRadar: String,
root_build: String,
inserted_by: String,
milestone: String,
applicable_chipsets: [String],
project_tag: String,
gerrits:String,
inserted_on:Date,
SDK:String
},
{
collection: 'radar_life_cycle',
timestamps: { createdAt: true, updatedAt: true },
});
module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);