Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:24:29+00:00 2026-05-16T22:24:29+00:00

Trying to insert into a mongodb database from scala. the below codes dont create

  • 0

Trying to insert into a mongodb database from scala. the below codes dont create a db or collection. tried using the default test db too. how do i perform CRUD operations?

object Store {
      def main(args: Array[String]) = {
        def addMongo(): Unit = {
          var mongo = new Mongo()
          var db = mongo.getDB("mybd")
          var coll = db.getCollection("somecollection")
          var obj = new BasicDBObject()
      obj.put("name", "Mongo")
      obj.put("type", "db")
      coll.insert(obj)
      coll.save(obj)
      println("Saved") //to print to console
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:24:29+00:00Added an answer on May 16, 2026 at 10:24 pm

    On a first glance things look OK in your code although you have that stray def addMongo(): Unit = {
    code at the top. I’ll defer to a suggestion on looking for errors here…. Two items of note:

    1) save() and insert() are complementary operations – you only need one. insert() will always attempt to create a new document … save() will create one if the _id field isn’t set, and update the represented _id if it does.

    2) Mongo clients do not wait for an answer to a write operation by default. It is very possible & likely that an error is occurring within MongoDB causing your write to fail. the getLastError() command will return the result of the last write operation on the current connection. Because MongoDB’s Java driver uses connection pools you have to tell it to lock you onto a single connection for the duration of an operation you want to run ‘safely’ (e.g. check result). This is the easiest way from the Java driver (in Scala, sample code wise, though):

       mongo.requestStart() // lock the connection in
       coll.insert(obj) // attempt the insert
       getLastError.throwOnError() // This tells the getLastError command to throw an exception in case of an error
       mongo.requestDone() // release the connection lock
    

    Take a look at this excellent writeup on MongoDB’s Write Durability, which focuses specifically on the Java Driver.

    You may also want to take a look at the Scala driver I maintain (Casbah) which wraps the Java driver and provides more scala functionality.

    We provide among other things an execute-around-method version of the safe write concept in safely() which makes things a lot easier for testing for writes’ success.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert a large amount of text into a mongodb collection from
I'm trying to insert some data from a proprietary JSON database into MongoDB for
I'm trying to insert into a database with the code below, I've tried everything
I am trying to insert a whole Java object into a MongoDB Collection using
I am trying to insert into my database table using the database helper class.
I am trying to insert data into a PostgreSQL database table using Python. I
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
I was trying to insert values into an Access database using a parameterized query:
hi am trying to insert values into SQL database using jdbc. Current code able
I'm trying to authenticate before using ajax to insert into a database $('#button').click(function ()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.