473,699 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MongoDB Upsert

8 New Member
Hello,

I have nested BSON documents inside an array in the document. I would like to be able to add/update items without having to do so many queries to check if it exists. Is there an easier/more efficient way to do it? This is going to be done a few million times every hour.

Example structure

Expand|Select|Wrap|Line Numbers
  1. /* 0 */
  2. {
  3.   "_id" : ObjectId("5205344a405a280bf8f3b1c5"),
  4.   "mykey" : 1000,
  5.   "data" : [
  6.     {
  7.       "d" : 20130807
  8.       ...
  9.     }, 
  10.     {
  11.       "d" : 20130806
  12.       ...
  13.     }]
  14. }
  15.  

Example ------------------------

Expand|Select|Wrap|Line Numbers
  1. Dim query = New QueryDocument()
  2. query.Add(New BsonElement("mykey" mykeyid))
  3.  
  4. Dim count As Integer = mongo_collection.Count(query)
  5.  
  6. If count > 0 Then
  7.  
  8.     Dim query2 = New QueryDocument()
  9.     query2.Add(New BsonElement("mykey", mykeyid))
  10.     query2.Add(New BsonElement("data.d", [date]))
  11.  
  12.     count = mongo_collection.Count(query2)
  13.  
  14.     If count > 0 Then
  15.         mongo_collection.Update(query2, Update.Set("data.$", bson("data")(0)))
  16.     Else
  17.         mongo_collection.Update(query, Update.Push("data", bson("data")(0)))
  18.     End If
  19.  
  20.  
  21. Else
  22.   mongo_collection.Insert(bson)
  23. End If
  24.  
  25.  
Aug 9 '13 #1
0 1327

Sign in to post your reply or Sign up for a free account.

Similar topics

6
61578
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the same structure as follows Keycolumns ========== Material
4
3802
by: Robin Lawrie | last post by:
I've written an asp form that successfully takes a users firstname, surname, email address, username and password and then add's those details to an Access database. I've been trying to modify the code so that the script checks that the selected username doesn't already exist using the SQL IF EXIST command but I am getting the famous error as shown below: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
1
3376
by: Isaac Blank | last post by:
Hi. We've run into a concurrency issue I do not have a clear solution for. In a DB2 UDB 7.2 database, we have several tables with a chain of foreign key relarionships: Table1 primary key x table2 foreign key x references table1(x) table3 foreign key x references table2(x) .........................................
1
2437
by: mcmreddy | last post by:
Hello, Is there any advantage of using "MERGE INTO" SQL instead of using "EXISTS" AND "NOT EXISTS" in DB2?. Thanks, --Chandra
7
6851
by: What-a-Tool | last post by:
Remember seeing a post a while back from someone who wanted to add to a table only if the data didn't already exist. Someone suggested a type of query called an "Up" something or other. Can't remember - could someone refresh my memory on this please? Thanks
12
21076
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new record (3232,'Raindeer') based on the condition that the
0
3730
by: ancy | last post by:
hi, I am trying to do an upsert operation(insert/update) using merge function in oracle 8.1.7, But it doesnt work .What is the equivalent thing to merge in oracle 8.1.7
1
4030
by: Amit | last post by:
Hello , Is there any way i can import data from datagrid to sqlserver. please advise . thanks & Regards
6
3040
by: =?Utf-8?B?ZXhl?= | last post by:
Hi, I'm using TransactionScope to do a serie Insert, Update, Delete operations with the ExecuteNonQuery and some Selects with ExecuteScalar everything works fine with multiple connections in the same scope. Problem is that when i do two Selects with ExecuteReader (to fill datasets) it shows the folowing error: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the...
7
12066
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.