gpt4 book ai didi

node.js - Mongoose 种群忽略选项

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:54 25 4
gpt4 key购买 nike

我无法理解 Mongoose 的种群逻辑。

我有一个 UserSchema,其中包含一系列文章。

articles: {
type: [{
article: {
type: Schema.Types.ObjectId,
ref: "Article"
},
read: Boolean,
starred: Boolean
}]
},

文章存储在单独的集合中。现在我想找到用户的所有加星标的文章,填充这些文章,按发布日期对它们进行排序并对结果进行分页。

User.findOne({_id: user.id, "articles.starred": true}).populate({path: "articles.article", options: {sort: {"published": -1}, skip: 20, limit: 20}}).exec(function(err, _user) {
callback(_user);
});

问题是,人口已经完成,但选项却被完全忽略了。

这是用户和文章的样子:

用户:

{
"__v" : 0,
"_id" : ObjectId("5284ef45fe46a55194000003"),
"articles" : [
{
"article" : ObjectId("5284ef52fe46a55194000005"),
"read" : true,
"starred" : true,
"_id" : ObjectId("5284ef53fe46a55194000ae1")
}
],
"categories" : [
{
"_id" : ObjectId("5284ef52fe46a55194000004"),
"feeds" : [
{
"url" : "http://feeds.dzone.com/zones/dotnet",
"name" : ".NET Zone - Community for .NET developers everywhere"
}
],
"name" : ".NET"
}
],
"email" : "xxxx",
"google" : {},
"name" : "xxxx",
"provider" : "xxxx"
}

文章:

{
"author" : "Michael Hession",
"title" : "You'll Never Want to Put Away This Elegant Pour-Over Coffee Dripper",
"published" : ISODate("2013-06-13T16:38:00.000Z"),
"link" : "http://gizmodo.com/youll-never-want-to-put-away-this-elegant-pour-over-co-513136433",
"feed" : {
"title" : "Gizmodo",
"link" : {
"html" : "http://gizmodo.com",
"xml" : "feed/http://feeds.gawker.com/gizmodo/excerpts.xml"
}
},
"summary" : "html content",
"_id" : ObjectId("5284ef52fe46a55194000005"),
"__v" : 0
}

最佳答案

由于我还找不到解决方案,所以我采取了解决方法。

User.findOne({_id: user.id, "articles.starred": false}).populate({path: "articles.article"}).exec(function(err, _user) {
if (!err && _user) {
var articles = _user.articles.sort(function(art1, art2) {
d1 = art1.article.published;
d2 = art2.article.published;

if (d1 > d2) return -1;
if (d1 < d2) return 1;

return 0;
}).slice(skip, skip + 20);

callback(articles);
} else {
callback([]);
};
});

如果有人提出更好的主意,请告诉我。

更新:

经过大量研究后,我更改了 UserSchema 并找到了一个更好的解决方案,它使用了 mongoose API 文档中解释的人口。

用户架构:

var UserSchema = new Schema({
name: {
type: String,
default: ""
},
email: {
type: String,
default: ""
},
provider: {
type: String,
default: ""
},
categories: {
type: [Category]
},
articles: {
read: [{type: Schema.Types.ObjectId, ref: "Article"}],
unread: [{type: Schema.Types.ObjectId, ref: "Article"}],
starred: [{type: Schema.Types.ObjectId, ref: "Article"}]
},
google: {},
facebook: {},
twitter: {}
});

获取Feed:

User.findById(user.id, "articles." + params.state, function(err, _user) {
User.populate(_user, {path: "articles." + params.state, options: {sort: {"published": -1}, skip: params.skip, limit: 20}}, function(_err, __user) {
if (!_err && __user) {
callback(__user.articles[params.state]);
} else {
callback([]);
};
});
});

关于node.js - Mongoose 种群忽略选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com