How to insert multiple records with OrientJS? - orientdb

I just started with OrientJS and OrientDB and I try to insert multiple records at the same time.
If I use this code nothing happens, no error, but no insertion either, what do i do wrong?
testRecords.map(el =>
db
.insert()
.into('Profile')
.set({
email: el.email,
password: el.password,
verifyEmailToken: el.verifyEmailToken,
changePasswordToken: el.changePasswordToken,
})
.one());
If I use the OrientDB SQL and the db.query then it works fine with this code below (but with this it looks kinda strange how I have to setup the values, one example value from the value array looks like this example '("testemail#test.com", "123", "123456789", "123456789")'
const OSQL = `INSERT INTO ${className} (${properties}) VALUES ${testValues}`;
console.log(OSQL);
db.query(OSQL);
Can somebody guide me how to use properly the OrientJS API?

Related

Does Mongo DB have functions similar to Oracle triggers

Dose mongo has some functions similar to oracle trigger? When i insert a document it can automatic update the fields createdTimestamp and modifiedTimestamp.
Excample:
After inserting/updating data:
{
'name': 'bobo',
'age': 17
}
final data will be updated as below. It means trigger dose time fields updating for me.
{
'name': 'bobo',
'age': 17,
'createdTimestamp': 2020-09-17T09:31:14.416+00:00,
'modifiedTimestamp': 2020-09-17T09:31:14.440+00:00
}
My solution is to use $currentDate and $setOnInsert, to update set upsert=true, code as below:
created_modified_timestamp_operation = {
'$currentDate': {
modifiedTimestamp: true
},
'$setOnInsert': {
createdTimestamp: new Date()
}
}
But for this solution I need to modify a lot of data operations,so I want to know is there any functions is similar to Oracle trigger, i just need to write a trigger to monitor whether the database is modified.
Thanks~~
Well triggers are only available in MongoDB atlas. But you can create triggers if you are using something like mongoose since it supports creating pre/post save, update triggers which I believe though are application level but can be of great help. And yes there are Change Streams you can read about them as well.

KMongo queries producing different results from Mongo Shell

I'm currently using KMongo and it's very nice. Simple to use, nice syntax, etc.
But I ran into an issue while querying some data that I cannot figure it out. I'm filtering for some fields and when I run my queries on Mongo Shell or robo 3T it works fine. Nevertheless, when I try to run on my Kotlin application, it (only in some cases) does not work. I'm querying like this:
collection.find(
MyEntity::name regex filter.name,
MyEntity::role eq filter.role,
)
But I also tried writing a string with the native query receiving the filtering value and I had the same issue. A concrete example is this query:
{ 'role': 'VENDOR', 'name': 'Internal Revenue Service'}
If I run on robo 3T like this:
db.getCollection('MyEntity').find({ 'role': 'VENDOR', 'name': 'Internal Revenue Service'})
I receive the results I expect, but if I run with KMongo, the exactly same query (and I doubled checked with the debugger), I receive nothing in result:
collection.find("{ 'role': 'VENDOR', 'name': 'Internal Revenue Service'}")
When I use regex to query (like in the first example), it seems to return only with small values: if I query for name with 'Internal Revenue Service' it produces no result, but if I query with only 'Internal' the result is correct.
Does anyone have any idea of what it could be? It seems deadly simple, but it's killing me that I can't figure it out.
I found out one day later that there was an injection of an offset that was wrongfully computed. So, when the query was more specific, it returned nothing.
That was kind of sad.

MongoDB Query - Filter only works with "_id"

I'm using mongodb v4.0.3, and this happens both with the shell and in compass.
This only happens with a certain collection. It previously had overriden ids (instead of the default mongodb id, there was a string. I dropped the collection and I recreated it without that).
I have the following structure ("mystructure"), for example:
{
"_id":ObjectId("5bd44eb528d61e3374b5e6ea"),
"custom_field":"data",
}
When I query it without a filter it returns all the docs:
db.mystructure.find({});
When I search for its objectid, it returns properly
db.mystructure.find( {"_id": ObjectId("5bd44eb528d61e3374b5e6ea")} );
But when I try to filter with any field, it doesn't return anything
db.mystructure.find( {"custom_field": "data"} );
At first I thought it would be solved recreating the collection with the automatically generated ids from mongodb but the problem persists. There are no "hidden" spaces in the string or anything like that. The same query in compass isn't working either. Other collections do work. It's on the same db, with the same user.
Why can this be?
Thank you very much.
you should write below code where 62c01e5a763d106152a2e53f is your _id
{_id:ObjectId('62c01e5a763d106152a2e53f')}

Is there a way to insert a DB query directly into a new MongoDB on server side?

What I'm trying to do is take a user's query client side (via search function), send it to server via Meteor methods and if there is a search result, insert that query into another MongoDB instance? I need this in order to display search results from the user into client.
Something like this from server side:
UserSearch: function(query){
var init = Table.find({userId: this.userId}, {data: {Name: query}});
Search.insert({userId: this.userId, data: init});
},
Obviously, that doesn't work. But I'm looking for a way to allow to insert a query from one database into a new database.
So I think there are two pieces two your question here:
How do I connect to another MongoDB instance?
How do I insert data from one query result into a collection?
For #1, referencing this SO question, you can do:
var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
Search = new Mongo.Collection("search", { _driver: database });
For #2, you're simply missing the .fetch() as MasterAM pointed out:
UserSearch: function(query){
var init = Table.find({userId: this.userId, 'data.Name': query}).fetch();
Search.insert({userId: this.userId, data: init});
},
I also modified your Table query a bit as I don't think it's working in the current form. You want each field property in a query object to be in the same object. Note that the fetch will give you an array of Table objects, so you'll need to have your data field in your Search collection reflect that.

Mongodb only insert if value is unique, else update - in node.js

I've started developing an app recently and have finally got my node.js server communicating with my mongodb database.
I want to insert a bunch a JSON objects that look something like this:
{
'Username': 'Bob',
'longitude': '58.3',
'latitude': '0.3'
}
If this Object is inserted into myCollection, and then I try to insert an object again with the Username Bob, but with different coordinates, I want the latest 'Username': 'Bob' object to replace the earlier one. There can only be one object in myCollection with the 'Username': 'Bob' basically.
If this was a relational database I would make Bob a primary key or something, but I was wondering what the best way to do this with mongoDb would be. Should I use the update+upsert method? I tried that and it didn't seem to work!
Apologies if this seems like a silly question, but I am still new to all of this.
Yes, a simple update query with the upsert option should satisfy your use case:
db.collection.update(
{username:"Bob"},
{$set:{'longitude': '58.3', 'latitude': '0.3'}},
{ upsert: true}
)
When you run the above query the first time (i.e., Bob doesn't exist in the collection), a new document is created. But when you run it the second time with new values for lat/long, the existing document is updated with the new lat/long values.
You can also create a unique index on the username field to prevent multiple records for 'Bob' from being created even accidentally:
db.collection.ensureIndex( { "username": 1 }, { unique: true } )
EDIT:
db.collection.ensureIndex() is now deprecated and is an alias for db.collection.createIndex(). So, use db.collection.createIndex() for creating indexes