有没有办法通过使用monk来简化内容(从mongodb数据中检索的列之一)?

发布于 2025-01-17 14:19:59 字数 863 浏览 1 评论 0原文

var monk = require('monk'); var db = monk('127.0.0.1:27017/db');
    
var collection = db.get('content'); newscol.find({}, {}).then((data) => {console.log(data);})

输出:

[
    {
    _id: 1,
    content: 'a b c d e f g h i j k l m n o p q r s t u v w x y z ',
    },
    {
    _id: 2,
    content: 'b c d e f g h i j k l m n o p q r s t u v w x y z',
    },
    {
    _id: 3,
    content: 'c d e f g h i j k l m n o p q r s t u v w x y z',
    }
]

尝试以正常方式检索数据。

但是有没有办法只检索如下内容的前几个单词?

[
    {
    _id: 1,
    content: 'a b c d...',
    },
    {
    _id: 2,
    content: 'b c d e...',
    },
    {
    _id: 3,
    content: 'c d e f....',
    }
]

我正在尝试使用 collection.aggregate 但似乎它对解决问题没有帮助。

var monk = require('monk'); var db = monk('127.0.0.1:27017/db');
    
var collection = db.get('content'); newscol.find({}, {}).then((data) => {console.log(data);})

Output:

[
    {
    _id: 1,
    content: 'a b c d e f g h i j k l m n o p q r s t u v w x y z ',
    },
    {
    _id: 2,
    content: 'b c d e f g h i j k l m n o p q r s t u v w x y z',
    },
    {
    _id: 3,
    content: 'c d e f g h i j k l m n o p q r s t u v w x y z',
    }
]

Tried to retrieve the data in a normal way.

But is there any way to retrieve only the first few words of the content as below?

[
    {
    _id: 1,
    content: 'a b c d...',
    },
    {
    _id: 2,
    content: 'b c d e...',
    },
    {
    _id: 3,
    content: 'c d e f....',
    }
]

I am trying to use the collection.aggregate but seems it doesn't help with the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-01-24 14:19:59
db.collection.aggregate([
  {
    $set: {
      content: {
        $reduce: {
          input: { $slice: [ { $split: [ "$content", " " ] }, 4 ] },
          initialValue: "",
          in: {
            $concat: [
              "$value",
              { $cond: [ { $eq: [ "$value", "" ] }, "", " " ] },
              "$this"
            ]
          }
        }
      }
    }
  }
])

mongoplayground

db.collection.aggregate([
  {
    $set: {
      content: {
        $reduce: {
          input: { $slice: [ { $split: [ "$content", " " ] }, 4 ] },
          initialValue: "",
          in: {
            $concat: [
              "$value",
              { $cond: [ { $eq: [ "$value", "" ] }, "", " " ] },
              "$this"
            ]
          }
        }
      }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文