Javascript 试图在meteor中合并两个收藏
我的应用程序中有两个集合,它们是从两个单独的json文件解析而来的。我已将这两个文件中的数据插入到单独的集合中。这些集合有相应的数字ID,我想在新集合中匹配它们。例如:postETA集合有一个Javascript 试图在meteor中合并两个收藏,javascript,mongodb,parsing,meteor,collections,Javascript,Mongodb,Parsing,Meteor,Collections,我的应用程序中有两个集合,它们是从两个单独的json文件解析而来的。我已将这两个文件中的数据插入到单独的集合中。这些集合有相应的数字ID,我想在新集合中匹配它们。例如:postETA集合有一个post\u id值,posts集合有一个相应的id 为了进一步解释这一点,这里有一个简单的集合示例。需要注意的是,有超过730个收集帖子,虽然有匹配的ID,但它们没有排序,所以当我查看它们时,它们彼此不匹配 发布收集示例: { "_id": "kTeQxenYZcQfPiaYv", "ID": "
post\u id
值,posts集合有一个相应的id
为了进一步解释这一点,这里有一个简单的集合示例。需要注意的是,有超过730个收集帖子,虽然有匹配的ID,但它们没有排序,所以当我查看它们时,它们彼此不匹配
发布收集示例:
{
"_id": "kTeQxenYZcQfPiaYv",
"ID": "44",
"post_content": "Today we talked about the letter Hh..."
}
{
"_id": "otEGQYxvv6MkCABST",
"post_id": "44",
"meta_value": "http://www.mrskitson.ca/wp-content/uploads/2010/11/snackTime.jpg"
}
postsmeta收集示例:
{
"_id": "kTeQxenYZcQfPiaYv",
"ID": "44",
"post_content": "Today we talked about the letter Hh..."
}
{
"_id": "otEGQYxvv6MkCABST",
"post_id": "44",
"meta_value": "http://www.mrskitson.ca/wp-content/uploads/2010/11/snackTime.jpg"
}
我想做的是解析集合,并以posts集合为例,其中ID
与postsmeta集合相匹配。找到匹配项后,我希望将集合内容(post_content
&meta_value
)插入新集合
这是到目前为止我所有的代码
lib/collections/posts.js
Postsmeta = new Mongo.Collection('postsmeta');
Posts = new Mongo.Collection('posts');
Meteor.publish('postsmeta', function() {
return Postsmeta.find();
});
Meteor.publish('posts', function() {
return Posts.find();
});
Meteor.startup(() => {
var postsmeta = JSON.parse(Assets.getText('postsmeta.json'));
var posts = JSON.parse(Assets.getText('posts.json'));
var length = postsmeta.length;
for(x=0; x < length; x++){
Posts.insert({
ID: posts[x].ID,
post_content: posts[x].post_content
});
Postsmeta.insert({
post_id: postsmeta[x].post_id,
meta_value: postsmeta[x].meta_value
});
}
});
server/publications.js
Postsmeta = new Mongo.Collection('postsmeta');
Posts = new Mongo.Collection('posts');
Meteor.publish('postsmeta', function() {
return Postsmeta.find();
});
Meteor.publish('posts', function() {
return Posts.find();
});
Meteor.startup(() => {
var postsmeta = JSON.parse(Assets.getText('postsmeta.json'));
var posts = JSON.parse(Assets.getText('posts.json'));
var length = postsmeta.length;
for(x=0; x < length; x++){
Posts.insert({
ID: posts[x].ID,
post_content: posts[x].post_content
});
Postsmeta.insert({
post_id: postsmeta[x].post_id,
meta_value: postsmeta[x].meta_value
});
}
});
server/main.js
Postsmeta = new Mongo.Collection('postsmeta');
Posts = new Mongo.Collection('posts');
Meteor.publish('postsmeta', function() {
return Postsmeta.find();
});
Meteor.publish('posts', function() {
return Posts.find();
});
Meteor.startup(() => {
var postsmeta = JSON.parse(Assets.getText('postsmeta.json'));
var posts = JSON.parse(Assets.getText('posts.json'));
var length = postsmeta.length;
for(x=0; x < length; x++){
Posts.insert({
ID: posts[x].ID,
post_content: posts[x].post_content
});
Postsmeta.insert({
post_id: postsmeta[x].post_id,
meta_value: postsmeta[x].meta_value
});
}
});
流星启动(()=>{
var postsmeta=JSON.parse(Assets.getText('postsmeta.JSON');
var posts=JSON.parse(Assets.getText('posts.JSON');
变量长度=postsmeta.length;
对于(x=0;x我希望我已经帮了忙,任何问题或疑问我都会在这里。拥抱 让我们稍微重构一下您的代码。我们将首先构建
Postsmeta
集合,然后联合创建Posts
和PostsCombined
集合。因为Postsmeta
已经存在,我们可以在它里面搜索匹配的文档
Meteor.startup(() => {
const postsmeta = JSON.parse(Assets.getText('postsmeta.json'));
postsmeta.forEach(doc => {
Postsmeta.insert({ post_id: doc.post_id, meta_value: doc.meta_value });
});
const posts = JSON.parse(Assets.getText('posts.json'));
posts.forEach(doc => {
const post = { ID: doc.ID, post_content: doc.post_content}
Posts.insert(post); // omit if you don't need the uncombined collection
const metadoc = Postsmeta.findOne({post_id: doc.ID}); // essentially a JOIN
if (metadoc) post.meta_value = metadoc.meta_value; // guard against no matching meta
PostsCombined.insert(post);
});
});
您的系统中不存在以下ID:
欢迎来到StackOverflow,祝贺你有勇气以新用户的身份发布答案。只是一个建议:试着在你的答案中加入实际的代码,并解释你为什么推荐一种特定的方法。请继续努力!抱歉,我不知道这是为了给出一个直接的答案,毫无疑问,我必须对它进行更多的研究,提高它的知识,但出于这个原因,我问了我到底想要什么,这是一个查询或插入,在插入的情况下,当你保存它时,它已经自动带来id,我仍然标记为-1,我的答案是正确的lol,但好吧,很抱歉回应…我不知道是谁否决了你的答案。请参阅“感激”,我将更新我的回复,以更清楚、更快地理解上下文,非常感谢您在这一行
Posts.insert({ID:doc.ID,post_content:doc.post_content})上的耐心和能力代码>我收到一个错误,说'(“
预期。不确定原因?在我的编辑器中,上面是我得到的错误,在应用程序本身中,我得到了这个错误server/main.js:30:64:意外标记,预期,
添加了缺少的括号汉克斯越来越近,现在我得到了一个错误TypeError:无法读取未定义的属性“meta_value”
,这意味着没有匹配的metadoc
用于该特定帖子。您确定原始json文件中的所有文档之间存在1:1的匹配吗?