Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用NodeJS执行MongoDB shell查询_Node.js_Mongodb - Fatal编程技术网

Node.js 如何使用NodeJS执行MongoDB shell查询

Node.js 如何使用NodeJS执行MongoDB shell查询,node.js,mongodb,Node.js,Mongodb,我知道我们在GitHub上提供了各种包,可以使用NodeJ查询MongoDB。它们不能解决我的用例。我希望从NodeJS执行mongodbshell命令,这与mongodbnodejs命令不同 示例:NodeJS db.collection('user').find({}) 示例:MongoDB Shell db.getCollection('user').find({}) 请注意,“collection”和“getCollection”是不同的 我想将MongoDB Shell查询保存为文

我知道我们在GitHub上提供了各种包,可以使用NodeJ查询MongoDB。它们不能解决我的用例。我希望从NodeJS执行mongodbshell命令,这与mongodbnodejs命令不同

示例:NodeJS

db.collection('user').find({})
示例:MongoDB Shell

db.getCollection('user').find({})
请注意,“collection”和“getCollection”是不同的


我想将MongoDB Shell查询保存为文本。从数据库中读取这些查询后,如何执行它们?

请尝试以下改编自MongoDB文档的方法:

const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const uri =
  "mongodb+srv://<user>:<password>@<cluster-url>?w=majority";
const client = new MongoClient(uri);
async function run() {
  try {
    await client.connect();
    const db = client.db("myDatabase");
    const result = await db.getCollection('user').find({});
    console.log(result);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);
const{MongoClient}=require(“mongodb”);
//用MongoDB部署的连接字符串替换uri字符串。
常量uri=
“mongodb+srv:/:@?w=多数”;
const client=新的MongoClient(uri);
异步函数run(){
试一试{
等待client.connect();
const db=client.db(“myDatabase”);
const result=await db.getCollection('user').find({});
控制台日志(结果);
}最后{
等待客户端。关闭();
}
}
run().catch(console.dir);

尝试改编自MongoDB文档:

const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const uri =
  "mongodb+srv://<user>:<password>@<cluster-url>?w=majority";
const client = new MongoClient(uri);
async function run() {
  try {
    await client.connect();
    const db = client.db("myDatabase");
    const result = await db.getCollection('user').find({});
    console.log(result);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);
const{MongoClient}=require(“mongodb”);
//用MongoDB部署的连接字符串替换uri字符串。
常量uri=
“mongodb+srv:/:@?w=多数”;
const client=新的MongoClient(uri);
异步函数run(){
试一试{
等待client.connect();
const db=client.db(“myDatabase”);
const result=await db.getCollection('user').find({});
控制台日志(结果);
}最后{
等待客户端。关闭();
}
}
run().catch(console.dir);

我想从NodeJS运行此查询,因为运行NodeJS的服务器上没有安装mongo db,所以不使用mongo命令是否可以执行此查询。@delsanic我已更新了我的答案,以更具体地回答您的问题。我收到此错误(节点:16367)未处理PromisejectionWarning:TypeError:db.getCollection不是函数。db.collection可以工作,但这就是问题所在,我想将查询存储为文本,然后稍后执行,由于运行NodeJS的服务器上没有安装mongo db,因此不使用mongo命令是否可以执行此操作。@delsanic我已更新了我的答案,以更明确地回答您的问题。我收到此错误(节点:16367)未经处理PromisejectionWarning:TypeError:db.getCollection不是函数。db.collection可以工作,但这就是问题所在,我想将查询存储为文本,然后稍后执行。示例:MongoDB Shell:db.getCollection('user')。find({})可以在节点程序的子进程内运行mongo Shell命令。要使用子进程运行,我需要在该系统上安装mongo。这是一个客户端应用程序,无法安装mongo。示例:MongoDB Shell:db.getCollection('user')。find({})您可以在节点程序的子进程内运行mongo Shell命令。要使用子进程运行,我需要在该系统上安装mongo。它是一个客户端应用程序,无法安装mongo。