MongoDB:插入具有特定id的文档而不是自动生成的ObjectID(MongoDB: insert documents with specific id instead of auto generated ObjectID)
我需要使用java在MongoDB上插入文档(使用特定的id而不是自动生成的ObjectID)。
要插入一个文档或更新(如果存在),我尝试使用
findOne
搜索id,如果它不存在则insert
id然后再insert
findAndModify
。 它有效,但我不觉得它是有效的方式,这是耗时的。 有没有更好的方法来实现这一目标?要一次插入多个文档,我正在遵循此解决方案 。 但我不知道如何插入我的自定义ID而不是objectID?
任何帮助将不胜感激
I need to insert documents on MongoDB (with specific id instead of auto generated ObjectID) using java..
To insert one document or update if exist, I tried use
findOne
to search for the id, if it doesn't exist theninsert
the id and thenfindAndModify
. It works but I don't feel that it's efficient way, it's time consuming. Is there a better way to achieve that?To insert multiple documents at once,I'm following this solution. but I don't know how I can insert my custom Id instead of objectID?
Any help will be appreciated
原文:https://stackoverflow.com/questions/26273821
最满意答案
提示:看看
self.class
在全局命名空间中定义它时,实际上是在Object类上定义方法,所有其他对象都从该类继承,因此
Fixnum
和String
继承新方法。 将它们放入类后,Fixnum
和String
不再定义这些方法。Hint: look at
self.class
When you define it in the global namespace you are actually defining methods on the Object class, from which all other objects inherit, so
Fixnum
andString
inherit your new methods. Once you put them into a class,Fixnum
andString
no longer have those methods defined.
相关问答
更多-
可以在类中定义Scala宏(作为该类的方法)吗?(Can Scala macros be defined inside a class (as methods of that class)?)[2023-07-21]
实现必须在对象或宏包中,但宏实现的方法可以在隐式类中。 注意val self = q"${c.prefix}.self"是获取对隐式类包装的对象的引用所必需的。 import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context object assertions { implicit class AssertEquals[T](val self: T) extends AnyVal { ... -
要在类实例中测试方法的存在,它应该是 expect(settingModal.saveForm).toBeDefined() 或者,更确切地说, expect(settingModal.saveForm).toBe(jasmine.any(Function)); To test the existence of a method in class instance it should be expect(settingModal.saveForm).toBeDefined() Or, more pre ...
-
class Foo def bar end end Foo.new.public_methods false => [:bar] class Foo def bar end end Foo.new.public_methods false => [:bar]
-
单行计算我在Ruby类中定义的方法数量?(One-liner to Count the Number of Methods I've defined in a Ruby Class?)[2023-11-12]
Admin.ancestors.each{|a| puts "For #{a} #{a.instance_methods(false)}"} Admin.ancestors.each{|a| puts "For #{a} #{a.instance_methods(false)}"} -
ruby-动态定义模块(ruby- define modules dynamically)[2021-12-12]
m = Object.const_set("Example1", Module.new) #=> Example1 m.define_singleton_method("ex_method") { 'example1_with' } #=> :ex_method 让我们来看看: Example1.is_a? Module #=> true Example1.methods.include?(:ex_method) #=> true Example1.ex_method #=> " ... -
我想这是因为do_something input self指针是InternshipInputFormatter ,而不是InternshipInputFormatter的实例。 所以调用helper_method(input)正确别名将是self.helper_method(input) ,但是你已经将Helper::MyHelper作为实例方法包含在InternshipInputFormatter类中,而不是单例,所以尝试使用实例方法扩展类该模块作为该类的signelton方法: class Inter ...
-
为什么允许在Ruby的类之外定义全局方法?(Why are global methods allowed to be defined outside of a class in Ruby?)[2024-03-12]
当以这种方式在全局范围内定义一个Ruby函数时,它在技术上将成为Object类的一个private方法,它是一切从Ruby继承的基类。 Ruby中的所有东西都是一个对象 ,所以确定你已经定义了一个方法。 def say_goodnight(name) result = "Goodnight, " + name return result end Object.private_methods.include? :say_goodnight => true 因为它被定义为一个在Object上 ... -
提示:看看self.class 在全局命名空间中定义它时,实际上是在Object类上定义方法,所有其他对象都从该类继承,因此Fixnum和String继承新方法。 将它们放入类后, Fixnum和String不再定义这些方法。 Hint: look at self.class When you define it in the global namespace you are actually defining methods on the Object class, from which all othe ...
-
我不知道原因,但行为是在Java语言规范#14.15中指定的: 打破没有标签 没有标签的break语句尝试将控制转移到最里面的封闭开关,while,do或者用于立即封闭的方法或初始化器的语句; 此语句称为中断目标,然后立即正常完成。 如果在立即封闭的方法,构造函数或初始化程序中没有switch,while,do或for语句包含break语句,则会发生编译时错误。 打破标签 (强调我的) 带有标签Identifier的break语句尝试将控制转移到与其标签具有相同标识符的封闭标签语句(第14.7节); 此语句 ...
-
MDN表示Firefox 19是最早支持page-break-inside属性的版本。 看起来你运气不好。 https://developer.mozilla.org/en-US/docs/CSS/page-break-inside 也许你可以从这个问题中找到有用的东西: 替换page-break-inside:避免因为兼容性问题 MDN says that Firefox 19 is the earliest version to support the page-break-inside propert ...