PDF (美式信紙) - 1.2Mb
PDF (A4) - 1.2Mb
使用 X DevAPI 時,使用文件集合非常簡單。以下範例顯示 CRUD 操作的基本用法(如需更多詳細資訊,請參閱第 4.3 節,「集合 CRUD 函數概述」):在建立與 MySQL Server 執行個體的連線之後,會建立一個新的集合,可以容納 JSON 文件,並插入數個文件。然後,執行尋找操作以搜尋集合中的特定文件。最後,再次從資料庫中捨棄集合。此範例假設 test
綱要存在,且 my_collection
集合不存在。
// Connecting to MySQL Server and working with a Collection
var mysqlx = require('mysqlx');
// Connect to server
var mySession = mysqlx.getSession( {
host: 'localhost', port: 33060,
user: 'user', password: 'password'} );
var myDb = mySession.getSchema('test');
// Create a new collection 'my_collection'
var myColl = myDb.createCollection('my_collection');
// Insert documents
myColl.add({ name: 'Laurie', age: 19 }).execute();
myColl.add({ name: 'Nadya', age: 54 }).execute();
myColl.add({ name: 'Lukas', age: 32 }).execute();
// Find a document
var docs = myColl.find('name like :param1 AND age < :param2').limit(1).
bind('param1','L%').bind('param2',20).execute();
// Print document
print(docs.fetchOne());
// Drop the collection
myDb.dropCollection('my_collection');