1

为了尝试深入了解流星并将其与 mongoDB 结合起来,我想问一个简单的问题。

示例提供了以下内容,如果数据库为空,则用于在数据库中填充信息。我要做的就是在开始时添加一行代码,就像 Lists.remove();在开始时一样。很简单,我可以继续玩这个例子,能够重新启动流星,重置数据库中的信息。我只是不确定数据集到底是什么。我知道使用上面列出的一个简单命令就可以做到这一点。我今天第一次看mongodb和meteor,所以谢谢你的原谅:)

// if the database is empty on server start, create some sample data.
Meteor.startup(function () {
if (Lists.find().count() === 0) {
var data = [
  {name: "Meteor Principles",
   contents: [
     ["Data on the Wire", "Simplicity", "Better UX", "Fun"],
     ["One Language", "Simplicity", "Fun"],
     ["Database Everywhere", "Simplicity"],
     ["Latency Compensation", "Better UX"],
     ["Full Stack Reactivity", "Better UX", "Fun"],
     ["Embrace the Ecosystem", "Fun"],
     ["Simplicity Equals Productivity", "Simplicity", "Fun"]
   ]
  },
  {name: "Languages",
   contents: [
     ["Lisp", "GC"],
     ["C", "Linked"],
     ["C++", "Objects", "Linked"],
     ["Python", "GC", "Objects"],
     ["Ruby", "GC", "Objects"],
     ["JavaScript", "GC", "Objects"],
     ["Scala", "GC", "Objects"],
     ["Erlang", "GC"],
     ["6502 Assembly", "Linked"]
     ]
  },
  {name: "Favorite Scientists",
   contents: [
     ["Ada Lovelace", "Computer Science"],
     ["Grace Hopper", "Computer Science"],
     ["Marie Curie", "Physics", "Chemistry"],
     ["Carl Friedrich Gauss", "Math", "Physics"],
     ["Nikola Tesla", "Physics"],
     ["Claude Shannon", "Math", "Computer Science"]
   ]
  }
];

var timestamp = (new Date()).getTime();
for (var i = 0; i < data.length; i++) {
  var list_id = Lists.insert({name: data[i].name});
  for (var j = 0; j < data[i].contents.length; j++) {
    var info = data[i].contents[j];
    Todos.insert({list_id: list_id,
                  text: info[0],
                  timestamp: timestamp,
                  tags: info.slice(1)});
    timestamp += 1; // ensure unique timestamp.
  }
}}});
4

2 回答 2

3

Lists.remove() 不起作用,因为您必须在其中指定某些内容,甚至什么都没有。Lists.remove({}) 是你想要的。

如文档http://docs.meteor.com/#remove中所述:

作为一项安全措施,如果选择器被省略(或未定义),则不会删除任何文档。如果您确实想从集合中移除所有文档,请将选择器设置为 {}。

所以:

Meteor.startup(function () {
  Lists.remove({});
  if (Lists.find().count() === 0) {
    console.log("Lists was empty");
    ...

注意:您将在启动流星的终端窗口中看到console.log,而不是浏览器控制台。

于 2013-10-16T19:14:02.437 回答
1

您可以在删除mongodb 数据meteor reset的命令行上使用

于 2013-10-16T19:04:52.527 回答