处理文档

Id的一代

在向集合添加文档时,如果文档不包含_id,它将自动分配一个连续的uuid类值。但是,可以通过提供静态的_id为每个文档。

使用自动分配的值

Const mysqlx = require('@mysql/xdevapi');Const docs = [];mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session =>{返回session. getschema ('mySchema').createCollection('myCollection');}) .then(collection =>{返回集合。Add ({name: 'foo'}) .execute() .then() =>{返回collection.find() .execute(doc => docs.push(doc));})}) .then(() => {// ' _id '值只是在这种情况下的一个例子console.log(docs);// [{_id: '00005a640138000000000000002c', name: 'foo'}]});

使用静态值

Const mysqlx = require('@mysql/xdevapi');Const docs = [];mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session =>{返回session. getschema ('mySchema').createCollection('myCollection');}) .then(collection =>{返回集合。Add ({_id: 1, name: 'foo'}) .execute() .then() =>{返回collection.find() .execute(doc => docs.push(doc));});}) .then(() => {console.log(docs);// [{_id: 1, name: 'foo'}]});

单文档CRUD

连接器提供了一组实用程序方法,可用于通过其添加、删除、替换或检索单个特定文档_id财产。

考虑一个集合mySchema.myCollection载有下列文件:

[{“_id”:“1”,“名字”:" foo "}, {" _id ":“2”,“名字”:“酒吧”}]

以下场景是可能的。

替换单个文档

如果一个文档具有给定的_id已经存在于数据库中,可以通过Collection.replaceOne ()方法。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。replaceOne('1', {name: 'baz', age: 23}) .then(result => {console.log(result. getaffecteemscount ());// 1返回collection.find() .execute();});}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',年龄:23},{_id:‘2’,名字:“酒吧”}]});

如果不存在这样的文档,该方法既不会失败,也不会产生任何效果。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema'). getcollection ('myCollection')返回集合。replaceOne('3', {name: 'baz', age: 23}) .then(result => {console.log(result. getaffecteemscount ());// 0返回collection.find() .execute();});}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“foo”},{_id:‘2’,名字:“酒吧”}]});

如果第二个参数中的替换文档包含_id属性及其值与作为第一个参数提供的id值不同,则将报告错误,而不管集合中是否已经存在具有这些id的文档。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema'). getcollection ('myCollection')返回集合。replaceOne('1', {_id: '2', name: 'baz',年龄:23})}).catch(err => {console.log(err.message);//替换文档的id与匹配文档的id不相同。});

创建或更新单个文档

连接器还提供了一种附加的实用方法-Collection.addOrReplaceOne ()-这允许无缝地创建一个文档与给定_id属性或自动替换现有的匹配文档。

因此,如果一个文档具有给定的_id已经存在于集合中,则行为与Collection.replaceOne ()

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。addOrReplaceOne('1', {name: 'baz', age: 23}) .then(result =>{//重新创建现有行(导致两个不同的操作)//参见//www.delbede.com/doc/refman/8.0/en/insert-on-duplicate.html consol10bet靠谱e.log(result. getaffecteemscount ());// 2返回collection.find() .execute();}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“记者”,年龄:23},{_id:‘2’,名字:“酒吧”}]});})

如果不存在这样的文档,则会创建一个新的文档。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。addOrReplaceOne('3', {name: 'baz', age: 23}) .then(result => {console.log(result. getaffecteditemscount ());// 1返回collection.find() .execute();}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“foo”},{_id:‘2’,名字:“酒吧”},{_id:“3”,名字:“记者”,年龄:23}]});});

就像replaceOne (),当调用addOrReplaceOne (),如果第二个参数中的替换文档包含_id属性及其值与作为第一个参数提供的id值不同,则将报告错误,而不管集合中是否已经存在具有这些id的文档。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。addOrReplaceOne('3', {_id: '4', name: 'baz',年龄:23})}).catch(err => {console.log(err.message);//替换文档的id与匹配文档的id不相同。});

当集合存在额外的惟一键约束时,会出现一些额外的场景。假设,的名字属性具有一个惟一的键约束自动生成列

ALTER TABLE mySchema。添加列名VARCHAR(3)生成的总是为(JSON_UNQUOTE(JSON_EXTRACT(doc, '$.name')))虚拟惟一键不是空的

现有的文档将用给定的属性更新,前提是与其他文档没有惟一的键约束冲突。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。addOrReplaceOne('1', {name: 'baz'}) .then(result =>{//重新创建现有行(导致两个不同的操作)//参见//www.delbede.com/doc/refman/8.0/en/insert-on-duplicate.html consol10bet靠谱e.log(result. getaffecteditemscount ());// 2返回collection.find() .execute();}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“记者”},{_id:‘2’,名字:“酒吧”}]});});

唯一键值本身也可以用相同的限制进行更新。

Const mysqlx = require('@mysql/xdevapi');Const docs = [];mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回集合。addOrReplaceOne('1', {name: 'foo', age: 23}) .then(result =>{//重新创建现有行(导致两个不同的操作)//参见//www.delbede.com/doc/refman/8.0/en/insert-on-duplicate.html consol10bet靠谱e.log(result. getaffecteemscount ());// 2返回collection.find() .execute();}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“foo”,年龄:23},{_id:‘2’,名字:“酒吧”}]});});

当然,违反惟一键约束会导致错误。

const mysqlx = require('@mysql/xdevapi') mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session =>{返回session. getschema ('mySchema'). getcollection ('myCollection')。addOrReplaceOne('1', {name: 'bar'});}) .catch(err => {console.log(err.message);})

检索单个文档

还有一个实用程序方法可以从集合中检索单个文档id-Collection.getOne ().方法返回一个承诺Which解析到文档实例(以字面对象的形式),如果它存在还是如果没有的话。

const mysqlx = require('@mysql/xdevapi') mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session =>{返回session. getschema ('mySchema').getCollection('myCollection').getOne('1');}) .then(doc => {console.log(doc);// {_id: '1', name: 'foo'}});
const mysqlx = require('@mysql/xdevapi') mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session =>{返回session. getschema ('mySchema').getCollection('myCollection').getOne('3');}) .then(doc => {console.log(doc);/ /空});

删除单个文档

还可以从给定的集合中删除特定的文档id-Collection.removeOne ().如果不存在这样的文档,则操作成功,但实际上什么也没有发生。

Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回collection.removeOne('1') .then(result => {console.log(result. getaffecteditemscount ());// 1返回collection.find() .execute();}) .then(result => {console.log(result. fetchall ());// [{_id: '2', name: 'bar'}]});});
Const mysqlx = require('@mysql/xdevapi');mysqlx. getsession ('mysqlx://localhost:33060/mySchema') .then(session => {const collection = session. getschema ('mySchema').getCollection('myCollection');返回collection.removeOne('3') .then(result => {console.log(result. getaffecteditemscount ());// 0返回collection.find() .execute();})}) .then(result => {console.log(result. fetchall ());/ / [{_id: ' 1 ',名字:“foo”},{_id:‘2’,名字:“酒吧”}]});