是否可以将 _id 字段内具有 _id :ObjectId("22332") 的 mongo 集合批量更新为字符串,以便 _id : "22332"改为使用
我正在使用 Meteor 并且我已经使用 mongoimport 导入了 csv 但它会自动将 ObjectId 添加为 ("22332") 我不想破坏默认的 meteor _id 功能并且必须将 ._str 在每个 javascript 文件上。
{
"_id" : ObjectId("5696cf153f8d6d60f723268b"),
"NTitle" : 18,
"field1" : "company announces Private Placement",
"field2" : "111099432820040602PP.pdf",
"field3" : "Vancouver, BC, June 2, 2004...... ",
"field4" : "2004-02-06",
"field5" : "YES"
}
我想要这个
{
"_id" : 5696cf153f8d6d60f723268b,
"NTitle" : 18,
"field1" : "company announces Private Placement",
"field2" : "111099432820040602PP.pdf",
"field3" : "Vancouver, BC, June 2, 2004...... ",
"field4" : "2004-02-06",
"field5" : "YES"
}
这是一个插入脚本。
Meteor.methods({
postInsert: function(postAttributes) {
check(Meteor.userId(), String);
check(postAttributes, {
title: String,
postContent: String,
timeToEnable: Date,
timeDisplay:String,
year: Number,
month: Number,
day: Number,
hours: Number,
timeString : String,
postNow: Date
});
var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username,
submitted: new Date()
});
var postId = Posts.insert(post);
return {
_id: postId
};
}
});
最佳答案
这是解决我的问题的粗略示例。
从 phpmyadmin 导出 csv
在本地安装 mongodb
运行
mongoimport -h localhost:3001 --db meteor --collection news4 --type csv --file env_news.csv --fields NID,NTitle,NPDF,NDesc,NAdded,NActive
server/changeData.js
var test = News4.find().fetch();
var test1 = News4Final.find().fetch();
for (i = 0; i < test.length; i++){
News4Final.insert({_id : test[i]._id._str}); //Run this Firsttime.
//Run everything below after first run
//var submitted = new Date(test[i].NAdded);
/*News4Final.update(test1[i]._id, {$set: {
title: test[i].NTitle,
PDF : test[i].NPDF,
postContent : test[i].NDesc,
submitted : submitted
}},{multi: false});*/
}
愚蠢的本地数据库
mongodump --port 3001
将数据库发送到 compose.io mongodb 服务器。
mongorestore --host caneww.60.mon342.com:10654 --db enwave -u admin -p password dump/meteor
使用 meteor up 并设置数据库环境变量。同时在 mup.json 中将 mongodb setup 关闭为 false
"MONGO_URL": "mongodb://admin:password@cand4ate.40.mongolayer.com:10644,candidate.12.mongolayer.com:11274/enwdve?replicaSet=set-569886d8c55a7400102b",
"MONGO_OPLOG_URL": "mongodb://admin:password@candi4te.40.mongolayer.com:10254,candidate.12.mongolayer.com:1d274/local?authSource=enwddve"
关于javascript - 将 MongoDB 集合 ObjectId 转换为 _id "string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801743/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file