PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb
使用 X DevAPI 时,处理文档集合非常简单。以下示例展示了在处理文档时 CRUD 操作的基本用法(有关更多详细信息,请参阅第 4.3 节“集合 CRUD 函数概述”):在建立与 MySQL 服务器实例的连接后,会创建一个可以保存 JSON 文档的新集合,并插入多个文档。然后,执行查找操作以从集合中搜索特定文档。最后,再次从数据库中删除该集合。该示例假设test
模式存在,并且my_collection
集合不存在。
Press CTRL+C to copy// 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');