【问题标题】:Mongoose array won't sort猫鼬数组不会排序
【发布时间】:2018-07-21 16:01:12
【问题描述】:

一个 mongodb 文档包含一个数组。当使用 mongoose 和 graphql 检索到前端时,对数组使用 .sort() 会引发错误。

无法分配给对象“[object Array]”的只读属性“1”

但是,在完全相同的数组(但这次是硬编码)上使用 .sort() 是可行的。

这里发生了什么?!

const c = [ { slug: 'first-element',
    position: 1,
    alphaPos: 'B',
    type: 'heading',
    data: 'first-el B' },
{ slug: 'second-element',
    position: 2,
    alphaPos: 'C',
    type: 'textarea',
    data: 'second-el C' },
{ slug: 'third-element',
    position: 3,
    alphaPos: 'A',
    type: 'textarea',
    data: 'third-el A' },
{ slug: 'third-b-element',
    position: 3,
    alphaPos: 'D',
    type: 'textarea',
    data: 'third-el-22222 D' } ]

    // 'content' is the same as 'c' but it comes from mongoose, 
    // it throws an error
    console.log('content ==', content.sort())

    // 'c' is hard coded, no error is thrown
    console.log('content ==', c.sort())

    // console.log(c) and console.log(content) output exactly the same  array

[编辑:这是 mongo 文档]

    {
        "_id" : ObjectId("5a801422f1c5437466c5d285"),
        "createdAt" : ISODate("2018-02-11T10:00:02.800Z"),
        "id" : 1,
        "slug" : "interesting-story",
        "creatorId" : 3,
        "title" : "An interesting story",
        "image" : "/images/image1.jpg",
        "description" : "description",
        "content" : [ 
            {
                "slug" : "first-element",
                "position" : 1,
                "alphaPos" : "B",
                "type" : "heading",
                "data" : "first-el B"
            }, 
            {
                "slug" : "second-element",
                "position" : 2,
                "alphaPos" : "C",
                "type" : "textarea",
                "data" : "second-el C"
            }, 
            {
                "slug" : "third-element",
                "position" : 3,
                "alphaPos" : "A",
                "type" : "textarea",
                "data" : "third-el A"
            }, 
            {
                "slug" : "third-b-element",
                "position" : 3,
                "alphaPos" : "D",
                "type" : "textarea",
                "data" : "third-el-22222 D"
            }
        ],
        "__v" : 0
    }

这是架构

const postSchema = new Schema({
    id: { type: Number, required: true, unique: true },
    slug: { type: String, required: true, unique: true },
    creatorId: { type: Number, required: true },
    title: { type: String, required: true },
    image: { type: String, required: false },
    description: { type: String, required: true },
    content: { type: Array, required: false },
    createdAt: { type: Date, default: Date.now },
})

【问题讨论】:

    标签: arrays node.js mongodb sorting


    【解决方案1】:

    如果有人遇到同样的错误,我仍然无法解释,但数组被认为是一个以索引为键的对象......

    如果有人对此行为有解释(与 graphql 相关?与 mongoose 相关?可能与 graphql-type-json 包相关?),我会很感兴趣。

    所以要对其进行排序,这就是我正在做的事情(输出一个数组)。

    Object.keys(content).sort(a=>1).reduce((_sortedObj, key) => {
        _sortedObj.push(content[key])
        return _sortedObj
        }, [])
    

    【讨论】:

      【解决方案2】:

      因为它们是 2 种不同的排序,取决于您所说的排序。如果你在数组(“硬编码”)上调用它,它使用默认排序函数https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort,如果你不能在不编写排序函数的情况下在 mongodocument 对象类型上调用它。

      要使用猫鼬排序,您必须在构建查询时应用它

      collection.find().sort().then(...)

      【讨论】:

      • 谢谢,但正如我所提到的,该数组包含在文档中,作为一个数组字段。如果内容是一个对象,.sort() 与 Object.keys(content).sort() 完美配合。在执行 console.log(content) 时,我可以清楚地看到 __ proto __ 中的 sort()
      • @Sbe88 可以尝试调用 .toObject()
      • 谢谢,但输出错误:content.toObject 不是函数。 PS:与此同时,我的猜测是架构是错误的……知道吗?尝试了几件事,例如 type: Schema.Types.Mixed, type: [] 但什么都没做
      • 我的意思是直接在 mongodocument (doc.toObject) 而不是 doc.content 上调用 toObject
      • 您是如何检索文档的?
      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2015-07-27
      • 2020-03-13
      • 2014-03-10
      • 1970-01-01
      • 2015-10-08
      相关资源
      最近更新 更多