小编Sus*_*t K的帖子

MongoDB $ text搜索如何工作?

我在事件集合中插入了以下值

db.events.insert(
   [
     { _id: 1, name: "Amusement Ride", description: "Fun" },
     { _id: 2, name: "Walk in Mangroves", description: "Adventure" },
     { _id: 3, name: "Walking in Cypress", description: "Adventure" },
     { _id: 4, name: "Trek at Tikona", description: "Adventure" },
     { _id: 5, name: "Trekking at Tikona", description: "Adventure" }
   ]
)
Run Code Online (Sandbox Code Playgroud)

我还通过以下方式创建了索引:

db.events.createIndex( { name: "text" } )
Run Code Online (Sandbox Code Playgroud)

现在,当我执行以下查询(搜索-步行)时:

db.events.find({
    '$text': {
        '$search': 'Walk'
    },
})
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

{ _id: 2, name: "Walk in Mangroves", description: "Adventure" }, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongodb-query

4
推荐指数
1
解决办法
545
查看次数

如何避免在 MongoDB 中使用 db.insertMany() 在集合中插入重复值?

询问:

db.getCollection('parents').insertMany(
    [{
        'name': 'Mark',
        'children': 'No Childs'
    },{
        'name': 'Carl',
        'children': '2'
    }], {
      'ordered': true,
    }
)
Run Code Online (Sandbox Code Playgroud)

我使用 mongoose 作为 ORM

有人可以建议我们是否有像 unique: true 这样的选项吗?我希望检查内容而不是 _id,因为它永远不会重复

注意:我尝试使用 upsert: true 进行 findOneAndUpdate,但这里我必须同时插入多个文档

mongoose mongodb node.js

2
推荐指数
1
解决办法
5650
查看次数

setTimeout 函数是否会影响 Node.js 应用程序的性能?

我有一个订票请求处理程序:

route.post('/makeBooking', (req, res) => {
  // Booking code

  setTimeout(function () {
    // Checks if the payment is made, if not then cancels the booking
  }, 900000);
});
Run Code Online (Sandbox Code Playgroud)

现在我有一条预订路线,如果未在 15 分钟内付款,超时功能将取消预订。

此功能会导致任何与性能相关的问题或内存泄漏吗?

javascript api node.js

2
推荐指数
1
解决办法
958
查看次数

标签 统计

node.js ×3

mongodb ×2

mongoose ×2

api ×1

javascript ×1

mongodb-query ×1