gpt4 book ai didi

javascript - 从 MongoDB 查询数据到 Javascript

转载 作者:行者123 更新时间:2023-12-01 01:15:54 25 4
gpt4 key购买 nike

我对编码很陌生,我需要查询在 MongoDB 中存储为 json 对象的数据。我读过有关nodejs的内容,但我很不确定如何开始。有没有人有好的阅读技巧或如何开始的描述?

//====================================================
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/PlotLayoutOptions';
const dbName = 'Vitboken';
var recievedObj = [];

//====================================================
// Use connect method to connect to the server
//====================================================
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client)
{
assert.equal(null, err);
console.log("Connected successfully to server");

const db = client.db(dbName);

//Query All Evaluation results
var query = {__Tag__: 'Evaluation'};
db.collection('Alt_0').find(query).toArray(function(err, result)
{
if (err) throw err;
recievedObj = result;
console.log(recievedObj);
});

//Close the client connection
client.close();
});

//====================================================

<!DOCTYPE html>
<head>
<title>DesignDashboard</title>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v4.js"></script><!-- Load d3.js -->
<script src="connectToMongo.js"></script>
<link href="Style.css" type="text/css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet"> <!--Google fonts -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui-ieshiv.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script scr="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"></script>
</head>

最佳答案

您可以从任何教程开始,就像我下面提到的那样。考虑到您了解 Javascript 的基础知识。

  • 第 1 步 -> 在 Node 中创建服务器
  • 第 2 步 -> 连接数据库
  • 第 3 步 -> 应用 CRUD 操作

Getting started with Node and Mongo

关于javascript - 从 MongoDB 查询数据到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com