Firebase 从Firestore导出json
由于我们可以在Firebase RTDB控制台下载json文件,是否有任何方法可以导出Firestore集合/文档数据的json文件Firebase 从Firestore导出json,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,由于我们可以在Firebase RTDB控制台下载json文件,是否有任何方法可以导出Firestore集合/文档数据的json文件 我的主要目标之一是在更新文档之前/之后比较数据。没有,您需要制定自己的流程,例如查询集合和循环所有内容 更新 截至2018年8月7日,我们有一个允许您将数据转储到GCS存储桶中的。虽然这不是JSON,但它的格式与云数据存储使用的格式相同,因此BigQuery可以理解它。这意味着您可以。Firestore仍处于开发初期,因此请查看以了解有关Firestore的任何信
我的主要目标之一是在更新文档之前/之后比较数据。没有,您需要制定自己的流程,例如查询集合和循环所有内容 更新
截至2018年8月7日,我们有一个允许您将数据转储到GCS存储桶中的。虽然这不是JSON,但它的格式与云数据存储使用的格式相同,因此BigQuery可以理解它。这意味着您可以。Firestore仍处于开发初期,因此请查看以了解有关Firestore的任何信息 我发现这个npm软件包很简单也很有用 请注意,
--accountCredentials path/to/credentials/file.json
指的是一个服务帐户密钥json文件,您可以按照中的说明获取该文件
我刚刚为Firestore编写了一个备份和恢复。你可以试试我的GitHub
谢谢,我编写了一个工具,可以遍历数据库的集合/文档,并将所有内容导出到单个json文件中。此外,它还将导入相同的结构(有助于克隆/移动Firestore数据库)。因为我有几个同事使用了这段代码,所以我想我会将它作为NPM包发布。请随意尝试并给出一些反馈
如果有人想要使用Python 2或3的解决方案 编辑:请注意,这不会备份规则 用叉子叉 首先安装并设置Firebase Admin Python SDK: 然后将其安装到python环境中:
pip install firebase-admin
安装Firestore模块:
pip install google-cloud-core
pip install google-cloud-firestore
(来自)
Python代码
# -*- coding: UTF-8 -*-
import firebase_admin
from firebase_admin import credentials, firestore
import json
cred = credentials.Certificate('xxxxx-adminsdk-xxxxx-xxxxxxx.json') # from firebase project settings
default_app = firebase_admin.initialize_app(cred, {
'databaseURL' : 'https://xxxxx.firebaseio.com'
})
db = firebase_admin.firestore.client()
# add your collections manually
collection_names = ['myFirstCollection', 'mySecondCollection']
collections = dict()
dict4json = dict()
n_documents = 0
for collection in collection_names:
collections[collection] = db.collection(collection).get()
dict4json[collection] = {}
for document in collections[collection]:
docdict = document.to_dict()
dict4json[collection][document.id] = docdict
n_documents += 1
jsonfromdict = json.dumps(dict4json)
path_filename = "/mypath/databases/firestore.json"
print "Downloaded %d collections, %d documents and now writing %d json characters to %s" % ( len(collection_names), n_documents, len(jsonfromdict), path_filename )
with open(path_filename, 'w') as the_file:
the_file.write(jsonfromdict)
npm i firebase admin
npm软件包谷歌让它变得比需要的更难,因此社区找到了一个解决办法。如果已安装
npm
,则可以执行以下操作:
出口
进口
打开任何客户端firebase应用程序(React、Angular等)。在任何地方使用此代码记录控制台并复制
const products = await db
.collection("collectionName")
.where("time", ">", new Date("2020-09-01"))
.get()
const json = JSON.stringify(products.docs.map((doc) => ({ ...doc.data() })))
console.log(json)
它对我有用
我曾经将Firestore中的所有数据导出为JSON格式。我使用的功能是:
exports.exportFirestore2Json = functions.https.onRequest((request, response) => {
db.collection("data").get().then(function(querySnapshot) {
const orders = [];
var order = null
querySnapshot.forEach(doc => {
order = doc.data();
orders.push(order);
});
response.send(JSON.stringify(orders))
return true
})
.catch(function(error) {
console.error("Error adding document: ", error);
return false
});
})
然后,去你会看到这样的东西
firestore导出/导入有npm 要导出的项目 转到->项目设置->服务帐户->生成新私钥->将其另存为exportedDB.json 要导入的项目 转到->项目设置->服务帐户->生成新私钥->将其另存为importedDB.json 从保存文件的文件夹中运行这两个命令 导出: npx-p节点firestore导入导出firestore导出-a exportedDB.json-b backup.json 导入:
npx-p节点firestore导入导出firestore导入-a importedDB.json-b backup.json是的,您可以,您不需要在firebase控制台中开始计费。有一个很好的npm包,您可以轻松地导出和导入firestore集合和文档。只需遵循以下步骤: -获取您的服务帐户密钥 打开Firebase控制台>项目设置>服务帐户>生成新私钥 使用serviceAccountKey.json重命名下载的文件 -现在创建一个新文件夹和index.js文件 -将servicekey.json粘贴到此文件夹中 -现在安装这个软件包
npm安装firestore导出导入
或
纱线添加firestore出口进口
从firebase导出数据
const{initializeApp}=require('firestore-export-import')
const servicecomport=require('./servicecomportkey.json')
常量appName=“[默认值]”
初始化EAPP(serviceAccount,appName)
常数fs=要求('fs');
const{backup}=require('firestore-export-import')
//备份('集合名称')
备份(“用户”)。然后((数据)=>
{
const json=json.stringify(数据);
//其中collection.json是您的输出文件名。
fs.writeFile('collection.json',json,'utf8',()=>{
console.log('done');
})
});
执行node index.js,您将看到一个新的collection.json文件,其中包含您的集合和文档。如果它看起来有点凌乱,请在网上格式化
这个index.js只是一个非常基本的配置,它可以导出整个集合中的所有内容,阅读它们的文档,您可以进行查询等等
将数据导入firebase
const{initializeApp,restore}=require('firestore-export-import')
const servicecomport=require('./servicecomportkey.json')
常量appName=“[默认值]”
初始化EAPP(serviceAccount,appName)
还原('collection.json'{
//其中refs是一个关键项数组
参考文献:[“用户”],
//自动纵火
npx -p node-firestore-import-export firestore-import -a credentials.json -b backup.json
const products = await db
.collection("collectionName")
.where("time", ">", new Date("2020-09-01"))
.get()
const json = JSON.stringify(products.docs.map((doc) => ({ ...doc.data() })))
console.log(json)
exports.exportFirestore2Json = functions.https.onRequest((request, response) => {
db.collection("data").get().then(function(querySnapshot) {
const orders = [];
var order = null
querySnapshot.forEach(doc => {
order = doc.data();
orders.push(order);
});
response.send(JSON.stringify(orders))
return true
})
.catch(function(error) {
console.error("Error adding document: ", error);
return false
});
})