使用嵌套 map 添加 Firestore 文档
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
DocumentReference docRef = db.Collection("data").Document("one");
Dictionary<string, object> docData = new Dictionary<string, object>
{
{ "stringExample", "Hello World" },
{ "booleanExample", false },
{ "numberExample", 3.14159265 },
{ "nullExample", null },
};
ArrayList arrayExample = new ArrayList();
arrayExample.Add(5);
arrayExample.Add(true);
arrayExample.Add("Hello");
docData.Add("arrayExample", arrayExample);
Dictionary<string, object> objectExample = new Dictionary<string, object>
{
{ "a", 5 },
{ "b", true },
};
docData.Add("objectExample", objectExample);
await docRef.SetAsync(docData);
Go
doc := make(map[string]interface{})
doc["stringExample"] = "Hello world!"
doc["booleanExample"] = true
doc["numberExample"] = 3.14159265
doc["dateExample"] = time.Now()
doc["arrayExample"] = []interface{}{5, true, "hello"}
doc["nullExample"] = nil
doc["objectExample"] = map[string]interface{}{
"a": 5,
"b": true,
}
_, err := client.Collection("data").Doc("one").Set(ctx, doc)
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
log.Printf("An error has occurred: %s", err)
}
Java
Map<String, Object> docData = new HashMap<>();
docData.put("stringExample", "Hello, World");
docData.put("booleanExample", false);
docData.put("numberExample", 3.14159265);
docData.put("nullExample", null);
ArrayList<Object> arrayExample = new ArrayList<>();
Collections.addAll(arrayExample, 5L, true, "hello");
docData.put("arrayExample", arrayExample);
Map<String, Object> objectExample = new HashMap<>();
objectExample.put("a", 5L);
objectExample.put("b", true);
docData.put("objectExample", objectExample);
ApiFuture<WriteResult> future = db.collection("data").document("one").set(docData);
System.out.println("Update time : " + future.get().getUpdateTime());
Node.js
const data = {
stringExample: 'Hello, World!',
booleanExample: true,
numberExample: 3.14159265,
dateExample: Timestamp.fromDate(new Date('December 10, 1815')),
arrayExample: [5, true, 'hello'],
nullExample: null,
objectExample: {
a: 5,
b: true
}
};
const res = await db.collection('data').doc('one').set(data);
PHP
$data = [
'stringExample' => 'Hello World',
'booleanExample' => true,
'numberExample' => 3.14159265,
'dateExample' => new Timestamp(new DateTime()),
'arrayExample' => array(5, true, 'hello'),
'nullExample' => null,
'objectExample' => ['a' => 5, 'b' => true],
'documentReferenceExample' => $db->collection('samples/php/data')->document('two'),
];
$db->collection('samples/php/data')->document('one')->set($data);
printf('Set multiple data-type data for the one document in the data collection.' . PHP_EOL);
Python
data = {
u'stringExample': u'Hello, World!',
u'booleanExample': True,
u'numberExample': 3.14159265,
u'dateExample': datetime.datetime.now(tz=datetime.timezone.utc),
u'arrayExample': [5, True, u'hello'],
u'nullExample': None,
u'objectExample': {
u'a': 5,
u'b': True
}
}
db.collection(u'data').document(u'one').set(data)
Ruby
doc_ref = firestore.doc "#{collection_path}/one"
data = {
stringExample: "Hello, World!",
booleanExample: true,
numberExample: 3.14159265,
dateExample: DateTime.now,
arrayExample: [5, true, "hello"],
nullExample: nil,
objectExample: {
a: 5,
b: true
}
}
doc_ref.set data
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。