Insert a document (C#)
Inserts a single document into a collection.
Documents are stored in collections. They represent a single row or record of data in Astra DB Serverless databases. For more information, see About collections with the Data API (C#).
If the collection is vector-enabled, pregenerated vector embeddings can be included by using the reserved $vector field.
If the collection has vectorize enabled, vector embeddings can be automatically generated from text specified in the reserved $vectorize field.
You can later use the $vector or $vectorize field to perform a vector search or hybrid search.
If the collection has lexical enabled, use the reserved $lexical field to store a string to index for lexicographical matching and the lexical search component of hybrid search.
Alternatively, you can use the $hybrid shorthand to populate the $vectorize and $lexical fields.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Inserts the specified document and returns a CollectionInsertOneResult object that includes the ID of the inserted document.
The ID value depends on the ID type. For more information, see Document IDs (C#).
Parameters
Use the InsertOneAsync method, which belongs to the Collection class.
You can also use InsertOne, which is the synchronous version of the method.
Method signature
public Task<CollectionInsertOneResult<TId>> InsertOneAsync(
T document,
CollectionInsertOneOptions options = null
);
| Name | Type | Summary |
|---|---|---|
|
|
An object describing the document to insert. A document can contain user-defined and reserved fields. User-defined field names can be any non-empty sequence of Unicode characters, with the following exceptions:
Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:
For examples, see Examples. With the C# client, if you specify a type for your documents instead of using the generic |
|
Optional. General API options for this operation, including the timeout. For more information and examples, see Customize API interaction. |
Examples
The following examples demonstrate how to insert a document into a collection.
Insert a document
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "title", "Hidden Shadows of the Past" },
{
"genres",
new List<string>
{
"Biography",
"Graphic Novel",
"Dystopian",
"Drama",
}
},
{
"metadata",
new Dictionary<string, object?>
{
{ "isbn", "978-1-905585-40-3" },
{ "language", "French" },
{ "edition", "Anniversary Edition" },
}
},
{ "number_of_pages", 245 },
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document with vector embeddings
Use the reserved $vector field to insert a document with pregenerated vector embeddings.
You can later use this field to perform a vector search.
All embeddings in the collection should use the same provider, model, and dimensions. Mismatched embeddings can cause inaccurate vector searches.
The $vector field is only supported for vector-enabled collections.
For more information, see Create a collection that can store vector embeddings and $vector in collections (C#).
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense.
For more information and examples, see Custom typing for collections.
Specifically, you can use the [DocumentMapping(DocumentMappingField.Vector)] attribute.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "$vector", new double[] { 0.08f, -0.62f, 0.39f } },
{ "name", "Jane Doe" },
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document and generate vector embeddings
Use the reserved $vectorize field to generate a vector embedding automatically. The value of $vectorize can be any string.
You can later use this field to perform a vector search.
The $vectorize field is only supported for collections that have vectorize enabled.
For more information, see Create a collection that can automatically generate vector embeddings and $vectorize in collections (C#).
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense.
For more information and examples, see Custom typing for collections.
Specifically, you can use the [DocumentMapping(DocumentMappingField.Vectorize)] attribute.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "$vectorize", "Text to vectorize" },
{ "name", "Jane Doe" },
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document for retrieval with hybrid search
|
Hybrid search and reranking are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms. |
If you plan to use hybrid search to find this document, the document must have both the $lexical field and the $vector field populated.
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense.
For more information and examples, see Custom typing for collections.
Specifically, you can use the [DocumentMapping(DocumentMappingField.Vector)], [DocumentMapping(DocumentMappingField.Vectorize)], [DocumentMapping(DocumentMappingField.Lexical)], and [DocumentMapping(DocumentMappingField.Hybrid)] attributes.
Example specifying the $vector and $lexical fields:
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "name", "Jane Doe" },
{ "$vector", new double[] { 0.08f, -0.62f, 0.39f } },
{
"$lexical",
"An athlete who loves biking, hiking, running, and swimming in the outdoors"
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Example specifying the $vectorize and $lexical fields:
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "name", "Jane Doe" },
{
"$vectorize",
"An athlete who loves biking, hiking, running, and swimming in the outdoors"
},
{
"$lexical",
"She shares her love of triathlons by coaching kids after school"
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Example using the $hybrid shorthand, which populates the $lexical and $vectorize field:
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "name", "Jane Doe" },
{
"$hybrid",
"An athlete who loves biking, hiking, running, and swimming in the outdoors"
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document for retrieval with lexicographical matching
|
Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms. |
If you plan to use lexicographical matching to find this document, the document must have the $lexical field populated.
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "name", "Jane Doe" },
{
"$lexical",
"An active hiker, runner, and triathlete who loves the outdoors."
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document and specify the ID
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense.
For more information and examples, see Custom typing for collections.
Specifically, you can use the [DocumentId] attribute.
Example generating a UUID:
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "_id", Guid.CreateVersion7() },
{ "name", "Jane Doe" },
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Example specifying an integer:
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "_id", 1 },
{ "name", "Jane Doe" },
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document with a binary field
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{
"exampleBinary",
new byte[]
{
0x3D,
0xFB,
0xE7,
0x6D,
0x3E,
0xE9,
0x78,
0xD5,
0x3F,
0x49,
0xFB,
0xE7,
}
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Insert a document with nested fields
Although you can use dot notation in a filter to find a document, you cannot use dot notation to insert a document. To specify nested fields in the inserted document, you must build a map, list, or set.
The following example uses untyped documents, but you can use strongly-typed classes for compile-time checks and IntelliSense. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Insert a document into the collection
var document = new Document()
{
{ "title", "Hidden Shadows of the Past" },
{
"genres",
new List<string>
{
"Biography",
"Graphic Novel",
"Dystopian",
"Drama",
}
},
{
"metadata",
new Dictionary<string, object?>
{
{ "isbn", "978-1-905585-40-3" },
{ "language", "French" },
{ "edition", "Anniversary Edition" },
}
},
};
var result = await collection.InsertOneAsync(document);
Console.WriteLine(result.InsertedId);
}
}
Client reference
For more information, see the client reference.