mongodb查询字段为空 mongodb查询xml

导读:MongoDB是一种非关系型数据库,支持多种数据格式的存储和查询 。本文将介绍如何使用MongoDB查询XML格式的数据 。
1. 将XML数据转换为JSON格式
在MongoDB中,数据以JSON格式存储和查询 。因此,我们需要将XML格式的数据转换为JSON格式 。可以使用xml2json库来完成这个任务 。
```
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const xmlData = 'http://data.evianbaike.com/MongoDB/John30';
parser.parseString(xmlData, (err, result) => {
if (err) console.log(err);
const jsonData = http://data.evianbaike.com/MongoDB/JSON.stringify(result);
console.log(jsonData);
});
2. 将JSON数据插入到MongoDB中
使用MongoDB的insert方法将JSON数据插入到集合中 。
const MongoClient = require('mongodb').MongoClient;
【mongodb查询字段为空 mongodb查询xml】const url = 'mongodb://localhost:27017/mydb';
MongoClient.connect(url, (err, db) => {
if (err) throw err;
const collection = db.collection('mycollection');
const jsonData = http://data.evianbaike.com/MongoDB/{ person: { name:'John', age: 30 } };
collection.insert(jsonData, (err, result) => {
if (err) throw err;
console.log(result);
db.close();
});
3. 查询XML数据
使用MongoDB的find方法查询XML数据 。
collection.find({ 'person.name': 'John' }).toArray((err, result) => {
总结:使用MongoDB查询XML数据需要将XML数据转换为JSON格式,并将JSON数据插入到MongoDB中 。之后,可以使用MongoDB的find方法查询XML数据 。

    推荐阅读