3
orientdb.insert()
        .into('User')
        .set({name: 'John', surname: 'Smith'})
        .all()
        .then(function(result) {
  ...
}, function(error){
  ...
})

This is the way to insert a single vertex in OrientDb via orientjs. How to insert multiple objects at once?

The following query

orientdb.insert()
        .into('User')
        .set([{name: 'John', surname: 'Smith'}, {name: 'Harry', surname: 'Potter'}])
        .all()
        .then(function(result) {
      ...
    }, function(error){
      ...
    })

inserts only the last element ({name: 'Harry', surname: 'Potter'})

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74

2 Answers2

2

You could also use the LET statement:

var OrientDB = require('orientjs');

var server = OrientDB({
    host: 'localhost',
    port: 2424,
    username: 'root',
    password: 'root'
});

var db = server.use({
    name: 'OrientJStest',
    username: 'root',
    password: 'root'
});

db.let('insert',function(i) {
            i
                .insert()
                .into('Person')
                .set({'name':'John'});
        })
        .let('insert2',function(i2) {
            i2
                .insert()
                .into('Person')
                .set({'name':'Harry'});
        })
        .commit()
        .all();

db.select().from('Person').all()
    .then(function (vertex) {
        console.log('Vertexes found: ',vertex);
});

OUTPUT:

Vertexes found:  [ { '@type': 'd',
    '@class': 'Person',
    name: 'John',
    '@rid': { [String: '#12:0'] cluster: 12, position: 0 },
    '@version': 1 },
  { '@type': 'd',
    '@class': 'Person',
    name: 'Harry',
    '@rid': { [String: '#12:1'] cluster: 12, position: 1 },
    '@version': 1 } ]

OUTPUT (STUDIO):

enter image description here

Hope it helps

LucaS
  • 1,418
  • 9
  • 12
1

Try this

var OrientDB = require('orientjs');

var server = OrientDB({
    host: 'localhost',
    port: 2424,
    username: 'root',
    password: 'root'
})

var db = server.use({
  name: 'mydb',
  username: 'admin',
  password: 'admin'
})

db.query('insert into User2(name) values ("John"),("Harry")').then(function (response) {
    console.log(response);
});

server.close();

This is the result that I get

enter image description here

Hope it helps

Michela Bonizzi
  • 2,622
  • 1
  • 9
  • 16