不显示任何节点索引的输出或错误。

huangapple go评论60阅读模式
英文:

does not show any output or error in indexes of node

问题

这是我用于创建索引并使用MongoDB查找数据的代码,但它没有显示任何输出或错误。

我已经安装了mongose、mongo-client,而且没有包含任何URL错误。哪里出错了?


// Connection URL and database name
const url = 'mongodb://127.0.0.1:27017';
const dbName = 'myproject';

// Use connect method to connect to the server
function connectDB(){

  MongoClient.connect(url, function(err, client) {
    if (err) {
      console.error('Failed to connect to the MongoDB server:', err);
      return;
    }
    console.log("Connected successfully to server");

    const db = client.db(dbName);
    const collection = db.collection('mycollection');

    // Create an index on the 'name' field
    collection.createIndex({name: Dev,age : 20}, function(err, result) {
      if (err) {
        console.error('Failed to create index:', err);
        console.log(result);
        client.close();
        return;
      }
      // Find all documents where the 'name' field is equal to 'John'
      collection.find({name: 'dev'}).toArray(function(err, docs) {
        if (err) {
          console.error('Failed to retrieve documents:', err);
          client.close();
          return;
        }
        console.log(docs);
        client.close();
      });
    });
  });
}

connectDB(); ```

<details>
<summary>英文:</summary>

This is my code for creating an index and finding the data using MongoDB, but it doesn&#39;t show any output or error.

I already install mongose, mongo-client, plus it does not contain any URL errors. Where did it go wrong?

const MongoClient = require('mongodb').MongoClient;

// Connection URL and database name
const url = 'mongodb://127.0.0.1:27017';
const dbName = 'myproject';

// Use connect method to connect to the server
function connectDB(){

MongoClient.connect(url, function(err, client) {
if (err) {
console.error('Failed to connect to the MongoDB server:', err);
return;
}
console.log("Connected successfully to server");

const db = client.db(dbName);
const collection = db.collection(&#39;mycollection&#39;);

// Create an index on the &#39;name&#39; field
collection.createIndex({name: Dev,age : 20}, function(err, result) {
  if (err) {
    console.error(&#39;Failed to create index:&#39;, err);
    console.log(result);
    client.close();
    return;
  }
  // Find all documents where the &#39;name&#39; field is equal to &#39;John&#39;
  collection.find({name: &#39;dev&#39;}).toArray(function(err, docs) {
    if (err) {
      console.error(&#39;Failed to retrieve documents:&#39;, err);
      client.close();
      return;
    }
    console.log(docs);
    client.close();
  });
});

});
}

connectDB();




</details>


# 答案1
**得分**: 0

你的代码存在一些问题:

1. createIndex 函数接受一个对象作为其参数,其中键是你想要索引的字段的名称,而值要么是1(升序),要么是-1(降序)。你的代码似乎传递了值 'Dev' 和 20,而不是 1 或 -1。

2. 在 find 函数中,你正在寻找 name 字段等于 'dev' 的文档,但你在 name 和 age 字段上创建了索引,其中 age 等于 20。这意味着该索引不会用于加速查询,如果集合中有大量文档,性能可能会较慢。

这应该在 name 和 age 字段上创建一个索引,其中 name 为升序,age 为降序。然后它会查找所有 name 字段等于 'dev' 的文档,并将它们输出到控制台。

<details>
<summary>英文:</summary>

There are a few issues with your code:

 1. The createIndex function takes an object as its parameter, where the keys are the names of the fields you want to index, and the values are either 1 (for ascending order) or -1 (for descending order). Your code seems to be passing the values &#39;Dev&#39; and 20 instead of 1 or -1.

 2. In the find function, you are looking for documents where the name field is equal to &#39;dev&#39;, but you created the index on the name and age fields, with age equal to 20. This means that the index will not be used to speed up the query, and the performance may be slow if you have a large number of documents in the collection.

const MongoClient = require('mongodb').MongoClient;

// Connection URL and database name
const url = 'mongodb://127.0.0.1:27017';
const dbName = 'myproject';

// Use connect method to connect to the server
function connectDB(){

MongoClient.connect(url, function(err, client) {
if (err) {
console.error('Failed to connect to the MongoDB server:', err);
return;
}
console.log("Connected successfully to server");

const db = client.db(dbName);
const collection = db.collection(&#39;mycollection&#39;);

// Create an index on the &#39;name&#39; and &#39;age&#39; fields
collection.createIndex({ name: 1, age: -1 }, function(err, result) {
  if (err) {
    console.error(&#39;Failed to create index:&#39;, err);
    client.close();
    return;
  }
  // Find all documents where the &#39;name&#39; field is equal to &#39;dev&#39;
  collection.find({ name: &#39;dev&#39; }).toArray(function(err, docs) {
    if (err) {
      console.error(&#39;Failed to retrieve documents:&#39;, err);
      client.close();
      return;
    }
    console.log(docs);
    client.close();
  });
});

});
}

connectDB();


This should create an index on the name and age fields, with name in ascending order and age in descending order. Then it will find all documents where the name field is equal to &#39;dev&#39; and output them to the console.

</details>



huangapple
  • 本文由 发表于 2023年4月17日 14:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定