我的 Meteor 1.1.0.2 应用程序中有以下(简化的)SimpleSchema 模式:
Tickers.attachSchema(
new SimpleSchema({
entries: {
type: [TickerEntries],
defaultValue: [],
optional: true
}
})
);
TickerEntries = new SimpleSchema({
id: {
type: String,
autoform: {
type: "hidden",
label: false,
readonly: true
},
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
text: {
type: String,
label: 'Text'
}
};
在数据库中我确实有以下条目:
{
"_id" : "ZcEvq9viGQ3uQ3QnT",
"entries" : [
{
"text" : "a",
"id" : "fc29774dadd7b37ee0dc5e3e"
},
{
"text" : "b",
"id" : "8171c4dbcc71052a8c6a38fb"
}
]
}
我想删除由 ID 指定的条目数组中的一个条目。
如果我在 meteor-mongodb-shell 中执行以下命令,它可以正常工作:
db.Tickers.update({_id:"3TKgHKkGnzgfwqYHY"}, {"$pull":{"entries": {"id":"8171c4dbcc71052a8c6a38fb"}}})
但问题是,如果我要在 Meteor 中做同样的事情,那是行不通的。这是我的代码:
Tickers.update({id: '3TKgHKkGnzgfwqYHY'}, {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
我还尝试了以下方法:
Tickers.update('3TKgHKkGnzgfwqYHY', {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
这些命令都不会给我错误,但它们不会从我的文档中删除任何内容。
有没有可能 $pull 命令没有得到正确支持,或者我在某处犯了错误?
提前致谢!
编辑:
我发现了在我的描述中看不到的问题,因为我已经简化了我的架构。在我的应用程序中,TickerEntries 中有一个附加属性 timestamp:
TickerEntries = new SimpleSchema({
id: {
type: String,
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
timestamp: {
type: Date,
label: 'Minute',
optional: true,
autoValue: function () {
if (!this.isSet) { // this check here is necessary!
return new Date();
}
}
},
text: {
type: String,
label: 'Text'
}
});
感谢 Kyll 的提示,我创建了一个 Meteorpad 并发现 autovalue 函数导致了问题。
我现在将函数更改为以下代码:
autoValue: function () {
if (!this.isSet && this.operator !== "$pull") { // this check here is necessary!
return new Date();
}
}
现在它正在运行。看起来,在拉动项目/对象的情况下返回自动值,它取消了拉动操作,因为该值未设置为返回值(因此时间戳属性保留旧值但未拉动).
下面是根据 Meteorpad 对其进行测试(只需在自动值函数中注释掉对运算符的检查):http://meteorpad.com/pad/LLC3qeph66pAEFsrB/Leaderboard
谢谢大家的帮助,你们所有的帖子都对我很有帮助!
最佳答案
对于基本的 meteor 应用程序,我称之为“bunk”。如果您创建一个全新的项目并简单地定义集合,那么 $pull运算符按预期工作:
控制台:
meteor create tickets
cd tickets
meteor run
然后打开一个 shell 并插入你的数据:
meteor mongo
> db.tickets.insert(data) // exactly your data in the question
然后只生成一些基本的代码和模板:
tickers.js
Tickers = new Meteor.Collection("tickers");
if (Meteor.isClient) {
Template.body.helpers({
"tickers": function() {
return Tickers.find({});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
tickers.html
<head>
<title>tickers</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
<ul>
{{#each tickers}}
{{> ticker}}
{{/each}}
</ul>
</body>
<template name="ticker">
<li>
{{_id}}
<ul>
{{#each entries}}
{{> entry }}
{{/each}}
</ul>
</li>
</template>
<template name="entry">
<li>{{ id }} - {{text}}</li>
</template>
应用程序应该运行良好,因此在您的浏览器控制台中执行 .update()(为了阅读而缩进):
Tickers.update(
{ "_id": "ZcEvq9viGQ3uQ3QnT" },
{ "$pull": { "entries": { "id": "fc29774dadd7b37ee0dc5e3e" } }}
)
并且该项目从条目中删除并且页面刷新时没有该项目。所以一切都消失了,正如预期的那样。
甚至添加 SimpleSchema和 Collection2包,这里没有区别:
meteor add aldeed:simple-schema
meteor add aldeed:collection2
tickers.js
Tickers = new Meteor.Collection("tickers");
TickerEntries = new SimpleSchema({
"id": {
type: String,
optional: true,
autoValue: function() {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str
}
}
},
"text": {
type: String
}
});
Tickers.attachSchema(
new SimpleSchema({
entries: { type: [TickerEntries] }
})
);
if (Meteor.isClient) {
Template.body.helpers({
"tickers": function() {
return Tickers.find({});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
重新初始化数据并在浏览器控制台中运行相同的命令,一切都保持不变。
检查这个或您自己操作中的任何打字错误或其他差异,以了解为什么这对您不起作用。
我强烈建议这样做,因为像这样的“重新开始”显示了预期的行为,如果您看到不同的行为,则可能是您安装的另一个插件有问题。
但一般来说,这是可行的。
关于javascript - 通过 Meteor 从数组中提取条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853063/
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat