A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/cpp/crud/create/ below:

CRUD - Create - C++ SDK - Atlas Device SDKs

To create an object, you must instantiate it using the realm namespace. Move the object into the realm using the Realm.add() function inside of a write transaction.

When you move an object into a realm, this consumes the object as an rvalue. You must use the managed object for any data access or observation. In this example, copying the dog object into the realm consumes it as an rvalue. You can return the managed object to continue to work with it.

auto dog = realm::Dog{.name = "Rex", .age = 1};std::cout << "dog: " << dog.name << "\n";auto config = realm::db_config();auto realm = realm::db(std::move(config));auto managedDog = realm.write([&] { return realm.add(std::move(dog)); });

For more information about modeling an object, refer to: Define a New Object Type.

namespace realm {struct Dog {  std::string name;  int64_t age;};REALM_SCHEMA(Dog, name, age)}  

To create an embedded object, assign the raw pointer of the embedded object to a parent object's property. Move the parent object into the realm using the Realm.add() function inside of a write transaction.

In this example, we assign the raw pointer of the embedded object - ContactDetails * - to the embedded object property of the parent object - Business.contactDetails.

Then, we add the business object to the realm. This copies the business and contactDetails objects to the realm.

Because ContactDetails is an embedded object, it does not have its own lifecycle independent of the main Business object. If you delete the Business object, this also deletes the ContactDetails object.

auto config = realm::db_config();auto realm = realm::db(std::move(config));auto contactDetails = realm::ContactDetails{    .emailAddress = "email@example.com", .phoneNumber = "123-456-7890"};auto business = realm::Business();business._id = realm::object_id::generate();business.name = "MongoDB";business.contactDetails = &contactDetails;realm.write([&] { realm.add(std::move(business)); });

For more information about modeling an embedded object, refer to: Define an Embedded Object.

namespace realm {struct ContactDetails {      std::string emailAddress;  std::string phoneNumber;};REALM_EMBEDDED_SCHEMA(ContactDetails, emailAddress, phoneNumber)struct Business {  realm::object_id _id;  std::string name;  ContactDetails *contactDetails;};REALM_SCHEMA(Business, _id, name, contactDetails)}  

To create an object with a to-one relationship to another object, assign the raw pointer of the related object to the relationship property of the main object. Move the object into the realm using the Realm.add() function inside of a write transaction.

In this example, we assign the raw pointer of the related object - FavoriteToy * - to the relationship property of the main object - Dog.favoriteToy. Then, when we add the dog object to the realm, this copies both the dog and favoriteToy to the realm.

The related favoriteToy object has its own lifecycle independent of the main dog object. If you delete the main object, the related object remains.

auto config = realm::db_config();auto realmInstance = realm::db(std::move(config));auto favoriteToy = realm::FavoriteToy{    ._id = realm::uuid("68b696c9-320b-4402-a412-d9cee10fc6a5"),    .name = "Wubba"};auto dog = realm::Dog{    ._id = realm::uuid("68b696d7-320b-4402-a412-d9cee10fc6a3"),    .name = "Lita",    .age = 10,    .favoriteToy = &favoriteToy};realmInstance.write([&] { realmInstance.add(std::move(dog)); });

You can optionally create an inverse relationship to refer to the main object from the related object. For more information, refer to: Create an Object with an Inverse Relationship.

For more information about modeling a to-one relationship, refer to: Define a To-One Relationship.

struct FavoriteToy {  realm::primary_key<realm::uuid> _id;  std::string name;};REALM_SCHEMA(FavoriteToy, _id, name)struct Dog {  realm::primary_key<realm::uuid> _id;  std::string name;  int64_t age;    FavoriteToy* favoriteToy;};REALM_SCHEMA(Dog, _id, name, age, favoriteToy)

To create an object with a to-many relationship to one or more objects:

In this example, we append the raw pointers of the related objects - Employee * - to the relationship property of the main object - Company.employees. This creates a one-way connection from the Company object to the Employee objects.

Then, we add the Company to the realm. This copies the Company and Employee objects to the realm.

The related Employee objects have their own lifecycle independent of the main Company object. If you delete the main object, the related objects remain.

auto config = realm::db_config();auto realmInstance = realm::db(std::move(config));auto employee1 = realm::Employee{    ._id = 23456, .firstName = "Pam", .lastName = "Beesly"};auto employee2 = realm::Employee{    ._id = 34567, .firstName = "Jim", .lastName = "Halpert"};auto company =    realm::Company{._id = 45678, .name = "Dunder Mifflin"};company.employees.push_back(&employee1);company.employees.push_back(&employee2);realmInstance.write([&] { realmInstance.add(std::move(company)); });

You can optionally create an inverse relationship to refer to the main object from the related object. For more information, refer to: Create an Object with an Inverse Relationship.

For more information about modeling a to-many relationship, refer to: Define a To-Many Relationship.

namespace realm {struct Employee {  realm::primary_key<int64_t> _id;  std::string firstName;  std::string lastName;      std::string jobTitle_notPersisted;};REALM_SCHEMA(Employee, _id, firstName, lastName)}  
struct Company {  int64_t _id;  std::string name;        std::vector<Employee*> employees;};REALM_SCHEMA(Company, _id, name, employees)

To create an object with a inverse relationship to another object, assign the raw pointer of the related object to the relationship property of the main object. Move the object into the realm using the Realm.add() function inside of a write transaction.

In this example, we create two Person objects that each have a to-one relationship to the same Dog object. The Dog has an inverse relationship to each Person object. The inverse relationship backlink is automatically updated when a linked Person object updates its Dog relationship.

auto config = realm::db_config();auto realm = realm::db(std::move(config));auto dog = realm::Dog{.name = "Bowser"};auto [jack, jill] = realm.write([&realm]() {  auto person =      realm::Person{.name = "Jack", .age = 27, .dog = nullptr};  realm::Person person2;  person2.name = "Jill";  person2.age = 28;  person2.dog = nullptr;  return realm.insert(std::move(person), std::move(person2));});realm.write([&dog, jack = &jack]() { jack->dog = &dog; });CHECK(jack.dog->owners.size() == 1);realm.write([&dog, jill = &jill]() { jill->dog = &dog; });CHECK(jill.dog->owners.size() == 2);CHECK(jack.dog->owners.size() == 2);realm.write([jack = &jack]() { jack->dog = nullptr; });CHECK(jack.dog == nullptr);CHECK(jill.dog->owners.size() == 1);

For more information about modeling an inverse relationship, refer to: Define an Inverse Relationship.

struct Dog;struct Person {  realm::primary_key<int64_t> _id;  std::string name;  int64_t age = 0;  Dog* dog;};REALM_SCHEMA(Person, _id, name, age, dog)struct Dog {  realm::primary_key<int64_t> _id;  std::string name;  int64_t age = 0;  linking_objects<&Person::dog> owners;};REALM_SCHEMA(Dog, _id, name, age, owners)

When you create an object that has a map property, you can set the values for keys in a few ways:

auto config = realm::db_config();auto realm = realm::db(std::move(config));auto employee = realm::Employee{    ._id = 8675309, .firstName = "Tommy", .lastName = "Tutone"};employee.locationByDay = {    {"Monday", realm::Employee::WorkLocation::HOME},    {"Tuesday", realm::Employee::WorkLocation::OFFICE},    {"Wednesday", realm::Employee::WorkLocation::HOME},    {"Thursday", realm::Employee::WorkLocation::OFFICE}};realm.write([&] {  realm.add(std::move(employee));  employee.locationByDay["Friday"] = realm::Employee::WorkLocation::HOME;});

Realm disallows the use of . or $ characters in map keys. You can use percent encoding and decoding to store a map key that contains one of these disallowed characters.

auto mapKey = "Monday.Morning";auto encodedMapKey = "Monday%2EMorning";

For more information about supported map data types, refer to: Map/Dictionary.

namespace realm {struct Employee {  enum class WorkLocation { HOME, OFFICE };  int64_t _id;  std::string firstName;  std::string lastName;  std::map<std::string, WorkLocation> locationByDay;};REALM_SCHEMA(Employee, _id, firstName, lastName, locationByDay)}  

You can create objects that contain set properties as you would any Realm object, but you can only mutate a set property within a write transaction. This means you can only set the value(s) of a set property within a write transaction.

auto realm = realm::db(std::move(config));auto docsRealmRepo =    realm::Repository{.ownerAndName = "mongodb/docs-realm"};auto managedDocsRealm =    realm.write([&]() { return realm.add(std::move(docsRealmRepo)); });auto openPullRequestNumbers = {3059, 3062, 3064};realm.write([&] {  for (auto number : openPullRequestNumbers) {                managedDocsRealm.openPullRequestNumbers.insert(number);  }});

For more information about supported set data types, refer to: Set.

namespace realm {struct Repository {  std::string ownerAndName;  std::set<int64_t> openPullRequestNumbers;};REALM_SCHEMA(Repository, ownerAndName, openPullRequestNumbers)}  

You can create an asymmetric object that syncs unidirectionally via Data Ingest to the Atlas database linked to your Atlas App Services App. You cannot access an asymmetric object locally, remove it from a realm, or query for it.

auto weatherSensorReading =    realm::WeatherSensorReading{.deviceId = "WX1278UIT",                                .temperatureInFahrenheit = 64.7,                                .windSpeedInMph = 7};realm.write([&] { realm.add(std::move(weatherSensorReading)); });

For more information about working with asymmetric objects, refer to Stream Data to Atlas - C++ SDK.

For more information about defining an asymmetric object, refer to: Define an Asymmetric Object.

struct WeatherSensorReading {  realm::primary_key<realm::object_id> _id{realm::object_id::generate()};  std::string deviceId;  double temperatureInFahrenheit;  int64_t windSpeedInMph;};REALM_ASYMMETRIC_SCHEMA(WeatherSensorReading, _id, deviceId,                        temperatureInFahrenheit, windSpeedInMph)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4